2010. január 10., vasárnap

How to extract all strings between a predetermined start and end point


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.


function TextBetweenStartAndEnd(const Text: string): string;
  pStart, pEnd: PChar;
  {sets a pointer to the "start" position}
  pStart := StrPos(PChar(Text), 'start');
  if Assigned(pStart) then
    {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);
  {if no "start" or "end" then raise an exception}
  if (not Assigned(pStart)) or (not Assigned(pEnd)) then
    raise Exception.Create('Error parsing text!');

Nincsenek megjegyzések:

Megjegyzés küldése