Recent

Author Topic: Splitting strings  (Read 1677 times)

schand99

  • Newbie
  • Posts: 4
Splitting strings
« 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
« Last Edit: October 05, 2020, 04:09:01 pm by schand99 »

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: Splitting strings
« Reply #1 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;

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: Splitting strings
« Reply #2 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.

Ally

  • Jr. Member
  • **
  • Posts: 50
Re: Splitting strings
« Reply #3 on: October 05, 2020, 04:48:36 pm »

bytebites

  • Hero Member
  • *****
  • Posts: 625
Re: Splitting strings
« Reply #4 on: October 05, 2020, 07:17:56 pm »
In this way you can ignore empty strings:
Code: Pascal  [Select][+][-]
  1. Test.Split(';',TStringSplitOptions.ExcludeEmpty);

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Splitting strings
« Reply #5 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.

The only true wisdom is knowing you know nothing

schand99

  • Newbie
  • Posts: 4
Re: Splitting strings
« Reply #6 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




trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: Splitting strings
« Reply #7 on: December 07, 2020, 10:00:31 pm »
Moved from Lazarus Release where it was off-topic.

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: Splitting strings
« Reply #8 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);

Sieben

  • Sr. Member
  • ****
  • Posts: 310
Re: Splitting strings
« Reply #9 on: December 08, 2020, 12:03:45 am »
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

 

TinyPortal © 2005-2018