Split a string in words lazarus / 61-Beginners ---------------------------------------------------------------------- funlw65 · 2022-01-05 03:43 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? ---------------------------------------------------------------------- tr_escape · 2022-01-05 03:55 Long time ago I was using this function: https://github.com/mehmetulukaya/muterm/blob/fa2d83ec5c7c327b1f03d5a3b 35b77c69e713058/MuLibs.pas#L213 But maybe there are built-in function about parsing words like as: https://www.freepascal.org/docs-html/rtl/strutils/splitstring.html ---------------------------------------------------------------------- funlw65 · 2022-01-05 04:33 Thank you for the links, I prefer not to use dynamic allocation... ---------------------------------------------------------------------- dsiders · 2022-01-05 05:05 Quote from: funlw65 on January 05, 2022, 05:33:20 amThank you for the links, I prefer not to use dynamic allocation... Then use TStringList and set its Delimiter and DelimitedText properties. ---------------------------------------------------------------------- Roland57 · 2022-01-05 06:24 Another possibility: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Uses StrUtils; var  i: integer;  Begin  { ... }  for i := 1 to WordCount(pigeon_record, [';']) do  begin    WriteLn(ExtractWord(i, pigeon_record, [';']));  end;End. ---------------------------------------------------------------------- Thaddy · 2022-01-05 07:25 Modern version: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program splitme;uses sysutils;var a:TstringArray;    b:AnsiString = 'Costica;Ana;Andrei;Ema';    c:AnsiString;begin  a:=b.Split([';']);  for c in a do writeln(c);end. BTW: what's wrong with the code editor? It now escapes  the string quotes. That was never the case.. [edit] txs for fixing ---------------------------------------------------------------------- bytebites · 2022-01-05 07:42 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program example;uses sysutils;var s:String;  c:char;begin  s:= 'Costica;Ana;Andrei;Ema';  for c in s do   if c=';' then writeln else write(c);  writeln;end. ---------------------------------------------------------------------- Thaddy · 2022-01-05 07:56 Mine can be a bit bit shorter and without branching in the maiin code; Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program splitme2;uses sysutils;var a:TstringArray;    b:AnsiString = 'Costica;Ana;Andrei';begin  a:=b.Split([';']);  for b in a do writeln(b); // re-use bend. ---------------------------------------------------------------------- PascalDragon · 2022-01-05 08:24 Quote from: funlw65 on January 05, 2022, 05:33:20 amThank 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 · 2022-01-05 09:18 @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. ---------------------------------------------------------------------- funlw65 · 2022-01-05 14:21 Wow, thank you all for the solutions! Quote from: PascalDragon on January 05, 2022, 09:24:52 amQuote from: funlw65 on January 05, 2022, 05:33:20 amThank 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 ---------------------------------------------------------------------- Thaddy · 2022-01-05 18:19 Quote from: funlw65 on January 05, 2022, 03:21:53 pmMicrocontroller 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.