Delphi: StringList Delimiter is always a space character even if Delimiter is set

DelphiDelphi 7DelimiterTstringlist

Delphi Problem Overview


I am having trouble with the delimiter in the TStringList Class. Take a look:

var
  s: string;
  sl: TStringList;

begin
  sl := TStringList.Create;
  s := 'Users^foo bar^bar foo^foobar^barfoo';
  sl.Delimiter := '^';
  sl.DelimitedText := s;
  ShowMessage(sl[1]);
end;

sl[1] SHOULD return 'foo bar'

sl[1] DOES return 'foo'

It seems that the delimiter is now '^' AND ' '

Any ideas?

Delphi Solutions


Solution 1 - Delphi

You should set s1.StrictDelimiter := True for spaces not to be considered delimiters, more info here.

Since you work in a version that does not support the above (as was clarified after the answer was submitted), you have two options:

  1. Find a character you know will not be used in the original text (e.g. underscore), convert all spaces to that character before splitting, and convert back after splitting. This is robosoft's suggestion.
  2. If you don't have inverted commas and spaces in the text, you can use Alexander's trick and wrap the text between delimiters in inverted command, so that 'hello hello^bye bye' turns to '"hello hello"^"bye bye"'. If you do choose this path and it works for you, please accept Alexander's answer and not mine, he also provides the code to implement it.

Both workarounds not using StrictDelimiter have limitations: the first requires some unused character, and the second requires no inverted commas and spaces in the original text.

Maybe it's time to upgrade to a newer version of Delphi :)

Solution 2 - Delphi

sl.DelimitedText := '"' + StringReplace(S, sl.Delimiter, '"' + sl.Delimiter + '"', [rfReplaceAll]) + '"';

Solution 3 - Delphi

Ryan has provider an excellent solution to this problem using ExtractStrings() function in Delphi. See:

https://stackoverflow.com/questions/3063930/parsing-a-string-using-a-delimiter-to-a-tstringlist-seems-to-also-parse-on-spac

So in your case, replace calls to sl.Delimiter and sl.DelimitedText with the line below: ExtractStrings(['^'], [], PChar(S), sl);

Solution 4 - Delphi

Work's in Delphi 7 "like gloves" for me. This is my function after apply Alexander's Trick:

procedure Split (const Delimiter: Char; Input: string; const Strings: TStrings) ;
begin
   Assert(Assigned(Strings)) ;
   Strings.Clear;
   Strings.Delimiter := Delimiter;
   Strings.DelimitedText :=  '"' + StringReplace(Input, Delimiter, '"' + Delimiter + '"', [rfReplaceAll]) + '"' ;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit1.Text := 'Users^foo bar^bar foo^foobar^barfoo';
  Split('^',Edit1.Text,Memo1.Lines);
end;

Thanks a lot!

Solution 5 - Delphi

sl.Text := StringReplace(S, sl.Delimiter, sLineBreak, [rfReplaceAll]);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAcronView Question on Stackoverflow
Solution 1 - DelphiRoee AdlerView Answer on Stackoverflow
Solution 2 - DelphiAlexView Answer on Stackoverflow
Solution 3 - DelphiKashif RajaView Answer on Stackoverflow
Solution 4 - DelphiAle CostaView Answer on Stackoverflow
Solution 5 - DelphibarbarisView Answer on Stackoverflow