Recent

Author Topic: POS function  (Read 22233 times)

Sora-Kun

  • Full Member
  • ***
  • Posts: 162
  • I can smell your presence ...
    • Sora-Kun
POS function
« on: June 07, 2010, 11:19:06 am »
Hello,
I'm trying to search for word in a TSYNEDIT, using the pos function, { ex: ch:= SynEdit1.Text; p:=pos(word,ch); }
but the function return 0. What's the probleme and how can I fix it. Thanks =)
if nothing suites you, make it your self!
The Revolution, Genesis. The next generation IDE.
If you want to help, PM me.

Made in Lazarus.
Soon, in The WWW.

cdbc

  • Hero Member
  • *****
  • Posts: 1026
    • http://www.cdbc.dk
Re: POS function
« Reply #1 on: June 07, 2010, 11:29:13 am »
Hi

When Pos-function returns 0, it means the substring you are searching for, isn't present in the source string.

hth
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Sora-Kun

  • Full Member
  • ***
  • Posts: 162
  • I can smell your presence ...
    • Sora-Kun
Re: POS function
« Reply #2 on: June 07, 2010, 11:34:42 am »
yeah I know that I have 2 years TURBO PASCAL experience on console and algorithme but I'm new to the graphic world in pascal.. however, the word exsists but the function return 0 here is the code :
procedure TForm1.BitBtn20Click(Sender: TObject);
 var x:integer;  msg,ch: string;
 begin
   ch:= SynEdit1.Text;
   word := search.Text;
   n:=0;
   x:=0;
  repeat
   begin
   x := pos(ch,word);
   str(x,msg);
   MessageDlg(msg,mtCustom, [mbOK], 0);
   n := n+1;
   tab[n]:= x;
   delete(ch,x,length(word));
   end;
  until (x = 0);
    if ( n <> 0 ) and ( n <> 1) then
    begin
      next.Enabled:=true;
     previous.Enabled:=true;
     end;
f:=n;
   str(n,msg);
   Label4.Caption:= (msg+' Word(s) found.');
   if (f = 1) then begin
      SynEdit1.SelStart:=(tab[n]+length(word)*n - length(word));
      SynEdit1.SelEnd:= (tab[n]+length(word)*n);
   end;
n:=1;
 end;

and when I see the Dialog message it contains 0.
Help  %)
if nothing suites you, make it your self!
The Revolution, Genesis. The next generation IDE.
If you want to help, PM me.

Made in Lazarus.
Soon, in The WWW.

mas steindorff

  • Hero Member
  • *****
  • Posts: 532
Re: POS function
« Reply #3 on: June 07, 2010, 11:37:05 am »
from the help file at:
http://www.freepascal.org/docs-html/rtl/system/pos.html

Description
Pos returns the index of Substr in S, if S contains Substr. In case Substr isn't found, 0 is returned. The search is case-sensitive.


The docs are good resource for simple items like this.  Do a search on "help" in Lazarus to see how the different resources available to you.  the simplest is to Highlight the keyword and press F1.
windows 10 &11, Ubuntu 21+ - fpc 3.0.4, IDE 2.0 general releases

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927
Re: POS function
« Reply #4 on: June 07, 2010, 11:44:22 am »
It's simple: Instead of trying to find the bug, you are posting to the forum.
You write in your first message:
p:=pos(word,ch);

But you do in your code:
x := pos(ch,word)


Sora-Kun

  • Full Member
  • ***
  • Posts: 162
  • I can smell your presence ...
    • Sora-Kun
Re: POS function
« Reply #5 on: June 07, 2010, 02:07:32 pm »
 >:( >:( >:( >:( >:( >:( >:( >:( >:( >:( >:( >:( >:( >:( >:( >:( lol yeah sorry  O:-) and thanks  8-)
if nothing suites you, make it your self!
The Revolution, Genesis. The next generation IDE.
If you want to help, PM me.

Made in Lazarus.
Soon, in The WWW.

 

TinyPortal © 2005-2018