Link to home
Start Free TrialLog in
Avatar of pr2501
pr2501

asked on

Counting caracters from cursor position in text to specific string.

Atached code count to positon when specific string in text is  present.
But in text i can have more times same  text is present:

example:one two three four five one six seven one

1. If i positon  cursor on the end of text then my result must be " 0 " and not " 38 " 
(number of characters to first time srched string is used).
2.if there is no srched string in text i  need to set a bolean varible "noText" to true.
3. if i have two lines when i put curor on the begining of second line it gives me two characters more than on the end of first line

What to do to resolve point 1,2 and 3?






procedure TForm1.Button1Click(Sender: TObject);
var cp: Integer;
    p: Integer;
begin
  p := Pos(Edit1.Text, Memo1.Lines.Text) + Length(Edit1.Text);
  cp := Memo1.SelStart;
  if cp >= p then
    Edit2.Text := IntToStr((cp - p)+1)
  else
    Edit2.Text := 'what should be here?';
end;

Open in new window

Avatar of Geert G
Geert G
Flag of Belgium image

hmm it is difficult to understand what you want

to start looking for text from a certain position use PosEx
Same as Pos but with a third parameter Offset

are you looking for a text from a certain position in a memo ?

cp := PosEx(Edit1.Text, Memo1.Text, Memo1.SelStart);

position of Edit1.Text in Memo1.Text and start looking from start of selection in Memo1
Avatar of pr2501
pr2501

ASKER

I have to look backwards from cursor position in text with more lines..
backwards ?
from the cursor position to the text ?
and give the beginning or the end of the text ?
and do you want the absolute position or the relative position to the cursor ?
Avatar of pr2501

ASKER


My text>
one two three four five  six seven one eight
If i srch for "one"> when i put cursor before "two" i must get 1 or if i put cursor before "thre" i must get 5. Then if i put cursor before "eighht" i must get 1. And if i srch for "ten" i must get 0.
 
use the FastStrings unit
this has routine for searching backwards:
http://www.koders.com/delphi/fidFB386C5C240FD5E72013C882ADD7600FDF60E6C7.aspx?s=socket

function FastPosBack(const aSourceString, aFindString : string; const aSourceLen, aFindLen, StartPos : Integer) : Integer;

this should help you out
ASKER CERTIFIED SOLUTION
Avatar of Geert G
Geert G
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pr2501

ASKER

Thank you


procedure TForm1.SpeedButton1Click(Sender: TObject);
 
var
 cp:integer ;
begin
cp := form1.FastPosBack(Memo1.Text, Edit1.Text, Length(Memo1.Text), Length(Edit1.Text), Memo1.SelStart);
   if cp > 0 then
   cp := Memo1.SelStart - (cp + Length(Edit1.Text));
   edit2.Text:=inttostr(cp);
end;

Open in new window