Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Retrieve specific text from TString

Posted on 2014-04-08
5
Medium Priority
?
285 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 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:Delphiwizard
ID: 39988099
Perfect, thank you.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

610 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