Recent

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

funlw65

  • Full Member
  • ***
  • Posts: 116
    • 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?

Lazarus 2.2.0rc3 fpc 3.2.2, Linux, UNIX BSD
widgetset: gtk2, msegui, qt5,  bgrabitmap controls
Immediate mode GUI: raygui 3.0

tr_escape

  • Sr. Member
  • ****
  • Posts: 410
  • 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: 116
    • 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...
Lazarus 2.2.0rc3 fpc 3.2.2, Linux, UNIX BSD
widgetset: gtk2, msegui, qt5,  bgrabitmap controls
Immediate mode GUI: raygui 3.0

dsiders

  • Hero Member
  • *****
  • Posts: 656
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 2.3.0 documentation at: https://dsiders.gitlab.io/lazdocsnext

Roland57

  • Sr. Member
  • ****
  • Posts: 267
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.

Thaddy

  • Hero Member
  • *****
  • Posts: 11637
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 »
Black themes should be banned.

bytebites

  • Sr. Member
  • ****
  • Posts: 481
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: 11637
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 »
Black themes should be banned.

PascalDragon

  • Hero Member
  • *****
  • Posts: 4168
  • 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: 11637
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.
Black themes should be banned.

funlw65

  • Full Member
  • ***
  • Posts: 116
    • 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 »
Lazarus 2.2.0rc3 fpc 3.2.2, Linux, UNIX BSD
widgetset: gtk2, msegui, qt5,  bgrabitmap controls
Immediate mode GUI: raygui 3.0

Thaddy

  • Hero Member
  • *****
  • Posts: 11637
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.
Black themes should be banned.

 

TinyPortal © 2005-2018