Lazarus

Free Pascal => General => Topic started by: schand99 on October 05, 2020, 04:06:35 pm

Title: Splitting strings
Post by: schand99 on October 05, 2020, 04:06:35 pm
Hello,
I have encountered this strange behaviour while Splitting string and counting lines:

 Var
   Test: String;
   SPTest: TStringArray;
   NumLines: Integer;
begin
   Test := 'Line1;Line2;Line3;Line4;';

   SPTest := Test.Split(';');
   NumLines := Length(SPTest);

   Numlines results 5 in Lazarus 2.0.10

In the previous Version 2.0.6 the result was 4

Is this an error?

I use Lazarus 64 bit on Windows 10 64 bit system
Title: Re: Splitting strings
Post by: Handoko on October 05, 2020, 04:39:54 pm
I quickly tested on my Lazarus 2.0.10 Linux GTK2, it showed the result = 5.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   Test:     string;
  4.   SPTest:   TStringArray;
  5.   NumLines: Integer;
  6. begin
  7.   Test     := 'Line1;Line2;Line3;Line4;';
  8.   SPTest   := Test.Split(';');
  9.   NumLines := Length(SPTest);
  10.   ShowMessage(NumLines.ToString);
  11. end;
Title: Re: Splitting strings
Post by: wp on October 05, 2020, 04:47:00 pm
The semicolon after "Line4" tells that there is a fifth empty string. When the old version fpc 3.0.4 counted only 4 strings it was in error.
Title: Re: Splitting strings
Post by: Ally on October 05, 2020, 04:48:36 pm
https://bugs.freepascal.org/view.php?id=35663
Title: Re: Splitting strings
Post by: bytebites on October 05, 2020, 07:17:56 pm
In this way you can ignore empty strings:
Code: Pascal  [Select][+][-]
  1. Test.Split(';',TStringSplitOptions.ExcludeEmpty);
Title: Re: Splitting strings
Post by: jamie on October 06, 2020, 02:02:41 am
In this way you can ignore empty strings:
Code: Pascal  [Select][+][-]
  1. Test.Split(';',TStringSplitOptions.ExcludeEmpty);

So what does that mean?

   1. Exclude all empty lines , starting , between and end ?

   2. Just the end ?

 Personally I don't think an empty line should be included if its at the end of the string because technically there is no data line there..

  But who am I?

 There should be an extra option to exclude the empty line at the end only.

Title: Re: Splitting strings
Post by: schand99 on October 06, 2020, 07:22:28 am
Hello again  :)

the "TStringSplitOptions.ExcludeEmpty" did not change the result of Length() in my example

   Test: String;
   SPTest: TStringArray;
   NumLines: Integer;
begin
   Test := 'Line1;Line2;Line3;Line4;';

   SPTest := Test.Split(';', TStringSplitOptions.ExcludeEmpty);
   NumLines := Length(SPTest);

NumLines is still 5



Title: Re: Splitting strings
Post by: trev on December 07, 2020, 10:00:31 pm
Moved from Lazarus Release where it was off-topic.
Title: Re: Splitting strings
Post by: ASerge on December 07, 2020, 11:54:53 pm
NumLines is still 5
Confirm:
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2. {$LONGSTRINGS ON}
  3. {$APPTYPE CONSOLE}
  4. uses SysUtils;
  5.  
  6. var
  7.   S: string;
  8.   A: TStringArray;
  9. begin
  10.   S := 'Line1;Line2;Line3;Line4;';
  11.   A := S.Split(';', TStringSplitOptions.ExcludeEmpty);
  12.   Writeln(Length(A));
  13.   for S in A do
  14.     Writeln('|', S, '|');
  15.   Readln;
  16. end.
Quote
5
|Line1|
|Line2|
|Line3|
|Line4|
||
But in FPC 3.3.1 already fixed:
Quote
4
|Line1|
|Line2|
|Line3|
|Line4|
and also TStringSplitOptions = (None, ExcludeEmpty, ExcludeLastEmpty);
Title: Re: Splitting strings
Post by: Sieben on December 08, 2020, 12:03:45 am
This discussion has already been continued here:

https://forum.lazarus.freepascal.org/index.php/topic,51706.msg380110.html#msg380110 (https://forum.lazarus.freepascal.org/index.php/topic,51706.msg380110.html#msg380110)
TinyPortal © 2005-2018