2010. január 10., vasárnap
How to extract all strings between a predetermined start and end point
Problem/Question/Abstract:
Could someone share some code that would extract all strings between 'start' and 'end'. I'm trying to load a document in TMemo and delete all strings not found inside a predetermined start and end point.
Answer:
function TextBetweenStartAndEnd(const Text: string): string;
var
pStart, pEnd: PChar;
begin
{sets a pointer to the "start" position}
pStart := StrPos(PChar(Text), 'start');
if Assigned(pStart) then
begin
{sets a pointer behind the "start" position}
Inc(pStart, Length('start'));
{looking for the "end" position}
pEnd := StrPos(pStart, 'end');
{copies the text between the "start" and "end" position}
if Assigned(pEnd) then
Result := Copy(string(pStart), 1, pEnd - pStart);
end;
{if no "start" or "end" then raise an exception}
if (not Assigned(pStart)) or (not Assigned(pEnd)) then
raise Exception.Create('Error parsing text!');
end;
Feliratkozás:
Megjegyzések küldése (Atom)
Nincsenek megjegyzések:
Megjegyzés küldése