Search code examples
delphidelphi-xe7

how do i extract first string from specific text


i need to extract string from a text as following example

Hi i have no name <z>empty</z>

i wanted to extract only text before <z> into array or string which is hi i have no name i tried this function

procedure Split (const Delimiter: Char; Input: string; const Strings: TStrings);
begin
   Assert(Assigned(Strings)) ;
   Strings.Clear;
   Strings.StrictDelimiter := true;
   Strings.Delimiter := Delimiter;
   Strings.DelimitedText := Input;
end;

but its only can split chars like ;,: etc.. i wanted to start split with this specific string <z>


Solution

  • As I read what you have written, you have a string and you want to ignore all text after the first occurrence of <z>. Use Pos and Copy for instance:

    P := Pos('<z>', input);
    if P = 0 then 
      output := input
    else 
      output := Copy(input, 1, P-1);
    

    Although something tells me that you really want an XML parser.