?
Solved

Retrieve specific text from TString

Posted on 2014-04-08
5
Medium Priority
?
288 Views
Last Modified: 2014-04-09
Hi,

What I want to do is to retrieve f.e. the name and date stored somewhere within a TString based on the preceding text.

Dear mister Long,

Client name: Sally Field
Date: 23/03/2014

With kind regards,

Open in new window


In this example a function would:
- Search for string "Client name: "
- If found, the function will return "Sally Field" (whole text after the searchstring)
- Search for string "Date: "
- If found, the function will return "23/03/2014" (whole text after the searchstring)

How can this be done?
0
Comment
Question by:Stef Merlijn
  • 3
  • 2
5 Comments
 
LVL 19

Expert Comment

by:Thommy
ID: 39986187
Try this...
function GetClientName(s:string):string;
var
  p: integer;
begin
  result:='';
  p:=pos('Client Name:',s);
  if p>0 then
    result:=Trim(Copy(s,p+Length('Client Name:'),Length(s)));
end;

function GetDate(s:string):string;
var
  p: integer;
begin
  result:='';
  p:=pos('Date:',s);
  if p>0 then
    result:=Trim(Copy(s,p+Length('Date:'),Length(s)));
end;

Open in new window

0
 
LVL 19

Expert Comment

by:Thommy
ID: 39986225
Or use one universal function...
function GetValueAfterPrecText(s:string;PrecText:string):string;
var
  p: integer;
begin
  result:='';
  p:=pos(PrecText,s);
  if p>0 then
    result:=Trim(Copy(s,p+Length(PrecText),Length(s)));
end;

...

s:=GetValueAfterPrecText('Client name: Sally Field','Client Name:'); //returns "Sally Field"
s:=GetValueAfterPrecText('Date: 23/03/2014'          ,'Date:');              //returns "23/03/2014"

Open in new window

0
 

Author Comment

by:Stef Merlijn
ID: 39986653
The function does position at the correct startingpoint, but it gets everything that comes after the search-text.
Dear mister Long,

Client name: Sally Field
Date: 23/03/2014

With kind regards,

Open in new window

In my example the GetValueAfterPrecText(MyText,'Client name: '); will return
Sally Field
Date: 23/03/2014

With kind regards,

Open in new window

It should only give me the text on the same line. In this case "Sally Field".
It should stop at the point of a hard of soft line-break.
0
 
LVL 19

Accepted Solution

by:
Thommy earned 2000 total points
ID: 39987984
I already wondered, but you said, you wanted the "whole text after the searchstring" and did not make any mention of line breaks!!!

Ok, then you have to search for the line breaks.
So this is your code...

function GetValueAfterPrecText(s:string;PrecText:string):string;
var
  p: integer;
begin
  result:='';
  s := StringReplace(s, #13, '', [rfReplaceAll]);

  p:=pos(PrecText,s);
  if p>0 then begin
    s:=Copy(s,p+Length(PrecText),Length(s));
    p:=pos(#10,s);
    if p>0 then begin
      result:=Trim(Copy(s,1,p-1));
    end;
  end;
end;

Open in new window

0
 

Author Closing Comment

by:Stef Merlijn
ID: 39988099
Perfect, thank you.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

839 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