How to split string by a multi-character delimiter?

awmross picture awmross · Mar 15, 2013 · Viewed 18.7k times · Source

Is there a Delphi function to split string by a multi-character delimiter rather than a single character ?

For instance when I'd use that function this way:

SplitString('Whale<->Mammal<->Ocean', '<->')

I would get a result of these 3 strings:

'Whale', 'Mammal', 'Ocean'

Is there such function in Delphi for this ?

Answer

Uwe Raabe picture Uwe Raabe · Mar 15, 2013

There is another quite simple solution using TStringList. Change the LineBreak:

procedure TForm208.Button1Click(Sender: TObject);
var
  lst: TStringList;
begin
  lst := TStringList.Create;
  try
    lst.LineBreak := '<->';
    lst.Text := 'Whale<->Mammal<->Ocean';
    Memo1.Lines := lst;
  finally
    lst.Free;
  end;
end;