Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split string by a multi-character delimiter?

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 ?

like image 588
awmross Avatar asked Mar 15 '13 03:03

awmross


2 Answers

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;
like image 130
Uwe Raabe Avatar answered Nov 01 '22 10:11

Uwe Raabe


You can check my StringUtils.pas unit that is part of Cromis Library

There is a simple text tokenizer there. But probably is just what you need. The interface is like that

TTextTokenizer = class
  private
    FTokens: TTokens;
    FDelimiters: array of ustring;
  public
    constructor Create;
    procedure Tokenize(const Text: ustring);
    procedure AddDelimiters(const Delimiters: array of ustring);
    property Tokens: TTokens read FTokens;
  end;

Suports strings as delimiters and also more then one delimiter.

like image 27
Runner Avatar answered Nov 01 '22 11:11

Runner