Recent

Author Topic: Split a string in words  (Read 5534 times)

funlw65

  • Full Member
  • ***
  • Posts: 148
    • Visual Pin Configurator for Nucleo 64pin boards
Split a string in words
« on: January 05, 2022, 04:43:02 am »
I have a string composed of four words of arbitrary length, separated by ";" character.
Example:
"Costica;Ana;Andrei;Ema"

Not a single word will be empty, so checking is not necessary.

I came with two methods, compiling the program with any of it activated, I get the same code size.

Any other method?

FreePascal 3.2.2, C 10.2.1, D 1.24 under Linux(init,musl,glibc), DragonflyBSD, NetBSD
gui: gtk2, qt5, raylib4.x+raygui3.x, nanovg 
tui: freevision, tvision2, termbox2+widgets, finalcut
db: typhoon-1.11...

tr_escape

  • Sr. Member
  • ****
  • Posts: 432
  • sector name toys | respect to spectre
    • Github:
Re: Split a string in words
« Reply #1 on: January 05, 2022, 04:55:19 am »

funlw65

  • Full Member
  • ***
  • Posts: 148
    • Visual Pin Configurator for Nucleo 64pin boards
Re: Split a string in words
« Reply #2 on: January 05, 2022, 05:33:20 am »
Thank you for the links, I prefer not to use dynamic allocation...
FreePascal 3.2.2, C 10.2.1, D 1.24 under Linux(init,musl,glibc), DragonflyBSD, NetBSD
gui: gtk2, qt5, raylib4.x+raygui3.x, nanovg 
tui: freevision, tvision2, termbox2+widgets, finalcut
db: typhoon-1.11...

dsiders

  • Hero Member
  • *****
  • Posts: 1052
Re: Split a string in words
« Reply #3 on: January 05, 2022, 06:05:16 am »
Thank you for the links, I prefer not to use dynamic allocation...

Then use TStringList and set its Delimiter and DelimitedText properties.
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

Roland57

  • Sr. Member
  • ****
  • Posts: 421
    • msegui.net
Re: Split a string in words
« Reply #4 on: January 05, 2022, 07:24:30 am »
Another possibility:

Code: Pascal  [Select][+][-]
  1. Uses StrUtils;
  2.  
  3. var
  4.   i: integer;
  5.  
  6. Begin
  7.   { ... }
  8.   for i := 1 to WordCount(pigeon_record, [';']) do
  9.   begin
  10.     WriteLn(ExtractWord(i, pigeon_record, [';']));
  11.   end;
  12. End.
My projects are on Gitlab and on Codeberg.

Thaddy

  • Hero Member
  • *****
  • Posts: 14213
  • Probably until I exterminate Putin.
Re: Split a string in words
« Reply #5 on: January 05, 2022, 08:25:52 am »

Modern version:

Code: Pascal  [Select][+][-]
  1. program splitme;
  2. uses sysutils;
  3. var a:TstringArray;
  4.     b:AnsiString = 'Costica;Ana;Andrei;Ema';
  5.     c:AnsiString;
  6. begin
  7.   a:=b.Split([';']);
  8.   for c in a do writeln(c);
  9. end.

BTW: what's wrong with the code editor? It now escapes  the string quotes. That was never the case..
[edit] txs for fixing
« Last Edit: January 05, 2022, 08:40:07 am by Thaddy »
Specialize a type, not a var.

bytebites

  • Hero Member
  • *****
  • Posts: 633
Re: Split a string in words
« Reply #6 on: January 05, 2022, 08:42:08 am »
Code: Pascal  [Select][+][-]
  1. program example;
  2. uses sysutils;
  3. var s:String;
  4.   c:char;
  5. begin
  6.   s:= 'Costica;Ana;Andrei;Ema';
  7.   for c in s do
  8.    if c=';' then writeln else write(c);
  9.   writeln;
  10. end.    

Thaddy

  • Hero Member
  • *****
  • Posts: 14213
  • Probably until I exterminate Putin.
Re: Split a string in words
« Reply #7 on: January 05, 2022, 08:56:57 am »
Mine can be a bit bit shorter and without branching in the maiin code;
Code: Pascal  [Select][+][-]
  1. program splitme2;
  2. uses sysutils;
  3. var a:TstringArray;
  4.     b:AnsiString = 'Costica;Ana;Andrei';
  5. begin
  6.   a:=b.Split([';']);
  7.   for b in a do writeln(b); // re-use b
  8. end.
« Last Edit: January 05, 2022, 09:31:55 am by Thaddy »
Specialize a type, not a var.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Split a string in words
« Reply #8 on: January 05, 2022, 09:24:52 am »
Thank you for the links, I prefer not to use dynamic allocation...

Why not? Are there specific reasons for that? Cause you're restricting yourself quite a bit without that.

Thaddy

  • Hero Member
  • *****
  • Posts: 14213
  • Probably until I exterminate Putin.
Re: Split a string in words
« Reply #9 on: January 05, 2022, 10:18:23 am »
@tr_escape
Splitstring is  - see your own link - a convenience wrapper around ansistring.split() so why not use string.split() directly? You then do not have to pull in the strutils unit.
Specialize a type, not a var.

funlw65

  • Full Member
  • ***
  • Posts: 148
    • Visual Pin Configurator for Nucleo 64pin boards
Re: Split a string in words
« Reply #10 on: January 05, 2022, 03:21:53 pm »
Wow, thank you all for the solutions!

Thank you for the links, I prefer not to use dynamic allocation...

Why not? Are there specific reasons for that? Cause you're restricting yourself quite a bit without that.

Microcontroller programming habits... I will save it for later, more complex projects in pascal.

And Print function of the forum comes in handy  :)
« Last Edit: January 05, 2022, 03:36:30 pm by funlw65 »
FreePascal 3.2.2, C 10.2.1, D 1.24 under Linux(init,musl,glibc), DragonflyBSD, NetBSD
gui: gtk2, qt5, raylib4.x+raygui3.x, nanovg 
tui: freevision, tvision2, termbox2+widgets, finalcut
db: typhoon-1.11...

Thaddy

  • Hero Member
  • *****
  • Posts: 14213
  • Probably until I exterminate Putin.
Re: Split a string in words
« Reply #11 on: January 05, 2022, 07:19:22 pm »
Microcontroller programming habits... I will save it for later, more complex projects in pascal.
In that case -microcontrollers - a more basic loop that does not rely on other units  like sysutils and strutils is the better solution.
Then you don't want to draw in so much code. (my code for example draws in a lot of code too. Most examples given here do so)
For mc's it is often better to do everything by hand, without relying on too many libraries.
For programming on e.g. windows and linux, the given examples can make your life much easier, though.
Specialize a type, not a var.

 

TinyPortal © 2005-2018