Search code examples
lazarus

Lazarus - parse function based on delimiter


I am building a small app in Lazarus and need a parse function based on the underscore. For example:

array := Split(string, delimiter);

So string = "this_is_the_first_post" and delimiter is the underscore resulting in the array being returned as:

array[0] = this
array[1] = is
array[2] = the
array[3] = first
array[4] = post

Any one has any idea how to go about this? I have tried a few code examples and it always throws an error.

Thanks.


Solution

  • You can use the following code:

    var
      List1: TStringList;    
    begin
      List1 := TStringList.Create;    
      try
        List1.Delimiter := '_';
        List1.DelimitedText := 'this_is_the_first_post';
    
        ShowMessage(List1[0]);
        ShowMessage(List1[1]);
        ShowMessage(List1[2]);
        ShowMessage(List1[3]);
        ShowMessage(List1[4]);
      finally
        List1.Free;
      end;
    end;
    

    In this example the output will be shown as a set of messages but you get the general idea.