Solved

Retrieve specific text from TString

Posted on 2014-04-08
5
273 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:Delphiwizard
[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
  • 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:Delphiwizard
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 500 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:Delphiwizard
ID: 39988099
Perfect, thank you.
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

717 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