Solved

Retrieve specific text from TString

Posted on 2014-04-08
5
261 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
  • 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now