Solved

Retrieve specific text from TString

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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
Intraweb submit form as a POST request 4 359
Delphi component that can load a DLL in design time? 8 93
Simple Delphi Question 9 106
RESTRequest Parameter 4 54
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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
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…

730 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