Sign in Join

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

Mailing lists / fpc-devel / 2018-09

String.Split inconsitent behaviour if Separators is [chars] vs [strings]

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Bart

Hi,

Consider this example:

program split;
{$mode objfpc}
{$h+}
uses
  sysutils;
var
  S: string;
  A: TStringArray;
  i: Integer;
begin
  S := '1 2 3 4 5';
  A := S.Split([#32],'"');
  writeln('S = ',S,', Separators = #32');
  for i := Low(A) to High(A) do writeln(format('%d: "%s"',[i,A[i]]));
  writeln;
  S := '1\n2\n3\n4\n5';
  A := S.Split(['\n'],'"');
  writeln('S = ',S,', Separators = \n');
  for i := Low(A) to High(A) do writeln(format('%d: "%s"',[i,A[i]]));
end.

Output:
S = 1 2 3 4 5, Separators = #32
0: "1"
1: "2"
2: "3"
3: "4"
4: "5"

S = 1\n2\n3\n4\n5, Separators = \n
0: "1"
1: "2"
2: "3"
3: "4"

I would expect the output would be the same for both versions of split? The "string" version misses the last one?

Bart

Reply by email

Sven Barth
to Bart

Bart schrieb am Sa., 29. Sep. 2018, 22:50:

Show quoted text 46 lines
Hi, Consider this example: program split; {$mode objfpc} {$h+} uses sysutils; var S: string; A: TStringArray; i: Integer; begin S := '1 2 3 4 5'; A := S.Split([#32],'"'); writeln('S = ',S,', Separators = #32'); for i := Low(A) to High(A) do writeln(format('%d: "%s"',[i,A[i]])); writeln; S := '1\n2\n3\n4\n5'; A := S.Split(['\n'],'"'); writeln('S = ',S,', Separators = \n'); for i := Low(A) to High(A) do writeln(format('%d: "%s"',[i,A[i]])); end. Output: S = 1 2 3 4 5, Separators = #32 0: "1" 1: "2" 2: "3" 3: "4" 4: "5" S = 1\n2\n3\n4\n5, Separators = \n 0: "1" 1: "2" 2: "3" 3: "4" I would expect the output would be the same for both versions of split? The "string" version misses the last one?

Please report as bug.

Regards,
Sven

Reply by email