Recent

Author Topic: The .Split function doesn't like mixed characters  (Read 290 times)

ASBzone

  • Hero Member
  • *****
  • Posts: 726
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
The .Split function doesn't like mixed characters
« on: May 25, 2025, 09:32:52 am »
I can get the Split function to split on Chr(10) and/or Chr(13) on a single pass.


I can get the Split function to split on '<' and/or '>' on a single pass.


If I try to make it check all 4 on a single pass, only Chr(10) and/or Chr(13) are evaluated.


Code: Pascal  [Select][+][-]
  1.  
  2. var
  3.   TempString: String;  // tried with AnsiString, and didn't help
  4.   TempArray: TStringArray;
  5.  
  6.  
  7. begin
  8.   TempString := 'abcdefgh<ijklmnopqr>stuvwxyxz' + chr(13) + '0123456789';
  9.  
  10.  
  11.   TempArray := TempString.Split('<>');
  12.   for I := Low(TempArray) to High(TempArray) do
  13.     WriteLn(I, ': ', TempArray[I]);
  14.   WriteLn('-------------', LINEENDING);
  15.  
  16.  
  17.   TempArray := TempString.Split(ansistring(Chr(10) + Chr(13)));
  18.   for I := Low(TempArray) to High(TempArray) do
  19.     WriteLn(I, ': ', TempArray[I], LINEENDING);
  20.   WriteLn('-------------', LINEENDING);
  21.  
  22.  
  23.   TempArray := TempString.Split(ansistring('<>' + Chr(10) + Chr(13)));
  24.   for I := Low(TempArray) to High(TempArray) do
  25.     WriteLn(I, ': ', TempArray[I]);
  26.   WriteLn('-------------', LINEENDING);
  27.  
  28.  
  29.   TempArray := TempString.Split(ansistring(Chr(60) + Chr(62) + Chr(10) + Chr(13)));
  30.   for I := Low(TempArray) to High(TempArray) do
  31.     WriteLn(I, ': ', TempArray[I], LINEENDING);
  32.   WriteLn('-------------', LINEENDING);
  33.  

Only the first two work properly.   The last two behave like the second one.


Versions in my sig...
-ASB: https://www.BrainWaveCC.com/

Lazarus v4.1.0.0 (c067bd336e) / FreePascal v3.2.3-1411-g8c665e3128 (aka fixes)
(Windows 64-bit install w/Win32 and Linux on ARM and x64 cross-compilers via FpcUpDeluxe)

My Systems: Windows 10/11 Pro x64 (Current)

Fibonacci

  • Hero Member
  • *****
  • Posts: 787
  • Internal Error Hunter
Re: The .Split function doesn't like mixed characters
« Reply #1 on: May 25, 2025, 10:05:04 am »
This is actually interesting:

Code: Pascal  [Select][+][-]
  1. procedure test(a: array of string);
  2. begin
  3.   writeln('len = ', length(a));
  4.   writeln('[0] = ', a[0]);
  5. end;
  6.  
  7. begin
  8.   test(string('abcde')); // [0] = abcde
  9.   //test('abcde');       // Error: Incompatible type for arg no. 1: Got "Constant String", expected "{Open} Array Of AnsiString"
  10.   readln;
  11. end.

As for Split(), the separators needs to be an array:

https://www.freepascal.org/docs-html/rtl/sysutils/tstringhelper.split.html

Code: Pascal  [Select][+][-]
  1. TempString.Split(ansistring(Chr(60) + Chr(62) + Chr(10) + Chr(13))); // 1-element String array
  2. // --->
  3. TempString.Split(['<', '>', #10, #13]); // array of char
  4. TempString.Split('<>#10#13');           // array of char
  5. TempString.Split(String('<>#10#13'));   // 1-element String array
« Last Edit: May 25, 2025, 10:19:23 am by Fibonacci »

ASBzone

  • Hero Member
  • *****
  • Posts: 726
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: The .Split function doesn't like mixed characters
« Reply #2 on: May 25, 2025, 04:29:55 pm »
This is actually interesting:

As for Split(), the separators needs to be an array:

https://www.freepascal.org/docs-html/rtl/sysutils/tstringhelper.split.html
....
Code: Pascal  [Select][+][-]
  1. TempString.Split(['<', '>', #10, #13]); // array of char
  2.  


This was super helpful.  I was about to embark on a far more complicated "solution"  😁


Thank you kindly.
-ASB: https://www.BrainWaveCC.com/

Lazarus v4.1.0.0 (c067bd336e) / FreePascal v3.2.3-1411-g8c665e3128 (aka fixes)
(Windows 64-bit install w/Win32 and Linux on ARM and x64 cross-compilers via FpcUpDeluxe)

My Systems: Windows 10/11 Pro x64 (Current)

 

TinyPortal © 2005-2018