Solved

Counting caracters from cursor position in text to specific string.

Posted on 2008-10-30
8
337 Views
Last Modified: 2010-04-05
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

0
Comment
Question by:pr2501
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22840093
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
0
 

Author Comment

by:pr2501
ID: 22840379
I have to look backwards from cursor position in text with more lines..
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22840393
backwards ?
from the cursor position to the text ?
and give the beginning or the end of the text ?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22840401
and do you want the absolute position or the relative position to the cursor ?
0
 

Author Comment

by:pr2501
ID: 22840480

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.
 
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22840781
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
0
 
LVL 37

Accepted Solution

by:
Geert Gruwez earned 250 total points
ID: 22840813
use it like

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

this will find the start of the text
if it returns non zero :

if cp > 0 then
  Result := Memo1.SelStart - (cp + Length(Edit1.Text));

0
 

Author Comment

by:pr2501
ID: 22855963
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

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi XE10 Round Image 2 231
Delphi: Connect to running MS Outlook 4 211
how to change, disabled button color FMX ? 1 66
Delphi: barcode reading on android platform 1 102
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question