Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2020-05

Is the code in wiki correct?

4 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Amir

Hi there,

  I was looking for a native implementation for JoinStrings and ended up in TStringList-TStrings_Tutorial
<https://wiki.freepascal.org/TStringList-TStrings_Tutorial>.

<https://wiki.freepascal.org/TStringList-TStrings_Tutorial>
In Conversion to and from delimited strings
<https://wiki.freepascal.org/TStringList-TStrings_Tutorial#Conversion_to_and_from_delimited_strings>
section, it claims the following code works if the separator is a string:
MyStringList:= TStringList.create;
MyStringList.SkipLastLineBreak := True;
MyStringList.add('1');
MyStringList.add('2');
MyStringList.add('3');
result := StringReplace(MyStringList.Text,Lineending,'\n',
[rfReplaceAll, rfIgnoreCase]);

I believe this is not correct. What if there is a newline(LineEnding) in one of the strings?

Best,
Amir

Reply by email

Michael Van Canneyt
to Amir
Amir wrote 21 lines
Hi there,   I was looking for a native implementation for JoinStrings and ended up in TStringList-TStrings_Tutorial <https://wiki.freepascal.org/TStringList-TStrings_Tutorial>. <https://wiki.freepascal.org/TStringList-TStrings_Tutorial> In Conversion to and from delimited strings <https://wiki.freepascal.org/TStringList-TStrings_Tutorial#Conversion_to_and_from_delimited_strings> section, it claims the following code works if the separator is a string: MyStringList:= TStringList.create; MyStringList.SkipLastLineBreak := True; MyStringList.add('1'); MyStringList.add('2'); MyStringList.add('3'); result := StringReplace(MyStringList.Text,Lineending,'\n', [rfReplaceAll, rfIgnoreCase]); I believe this is not correct. What if there is a newline(LineEnding) in one of the strings?

It will also be replaced. So yes, the code is wrong for such a case.

TStringlist is not a silver bullet, and it is definitely not a replacement for CSV-kind of functionality. If someone needs CSV, (s)he should use the TCSVDocument class.

Michael.

Reply by email

Amir
to Michael Van Canneyt

I assume StringJoin and StringSplit are one of the basic operations on Array of Strings and Strings. It looks like there is no easy way to use TStringList (that is claimed by the wiki to be a fancy "dynamic array or set of Strings"). These are not CSV-kind of functionalities...  Are these functionalities implemented in rtl?

Michael Van Canneyt wrote 40 lines
On Fri, 8 May 2020, Amir wrote: > Hi there, > >   I was looking for a native implementation for JoinStrings and ended > up in TStringList-TStrings_Tutorial > <https://wiki.freepascal.org/TStringList-TStrings_Tutorial>. > > > <https://wiki.freepascal.org/TStringList-TStrings_Tutorial> > In Conversion to and from delimited strings > <https://wiki.freepascal.org/TStringList-TStrings_Tutorial#Conversion_to_and_from_delimited_strings> > section, it claims the following code works if the separator is a > string: > MyStringList:= TStringList.create; > MyStringList.SkipLastLineBreak := True; > MyStringList.add('1'); > MyStringList.add('2'); > MyStringList.add('3'); > result := StringReplace(MyStringList.Text,Lineending,'\n', > [rfReplaceAll, rfIgnoreCase]); > > I believe this is not correct. What if there is a newline(LineEnding) > in one of the strings? It will also be replaced. So yes, the code is wrong for such a case. TStringlist is not a silver bullet, and it is definitely not a replacement for CSV-kind of functionality. If someone needs CSV, (s)he should use the TCSVDocument class. Michael. _______________________________________________ fpc-devel maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply by email

Michael Van Canneyt
to Amir
Amir wrote
I assume StringJoin and StringSplit are one of the basic operations on Array of Strings and Strings. It looks like there is no easy way to use TStringList (that is claimed by the wiki to be a fancy "dynamic array or set of Strings"). These are not CSV-kind of functionalities...

DelimitedText does what you need for join: it joins strings together with a separator. The limitation is that the separator must be a single char. We inherited this limitation from Delphi.

The example you pointed at, did more than join: it did a stringreplace, to work around this limitation, and the stringreplace was wrong.

 Are these functionalities implemented in rtl?

Join exists, it is "DelimitedText"; with abovementioned limitation.

Split exists as a type helper for string:
https://www.freepascal.org/docs-html/current/rtl/sysutils/tstringhelper.split.html

Michael.

Reply by email