[fpc-pascal] Split and Join

Juha Manninen juha.manninen at phnet.fi
Fri Nov 27 19:03:51 CET 2009


FPC's strutils or other libraries still don't have functions for splitting a 
string to a list and joining a list of strings again to one string, given a 
delimiter as a parameter.
Something like:
  function Spit(Str: string; Delim: string; Result: TStringList): integer;
  function Join(List: TStringList; Delim: string): string;

They don't exist in Delphi's libraries either. It is weird because every 
programmer at some point will need such functions.
Similar functions are created again and again by many people.
Such functions can be found from many utility libs around.

I found an archived fpc-pascal thread from over 2 years ago:
"splitting string into array"
The same issues came up there but the offered solutions were not optimized 
enough for FPC's magnificent library (!?!)
I think that is stupid! The basic functions for this job don't need to be 
highly optimized. The func signatures I gave above use TStringList which is 
good enough for most cases but not highly optimized. Also, a string as a 
delimiter is not optimized if your delimiter is actually 1 char.
Anyway, this kind of general case function is easy to implement.
Pos, Copy, Add...

Then there could be variations of it. For Split:
  function Spit(Str: string; Delim: char; Result: TStringList): integer;
  function Spit(Str: string; Delim: char; Result: array of string): integer;

The last one could really be optimized, minimizing the SetLength calls and so 

But now, I am making a demo program and need a simple Split functions. It can 
take 10 ms of 50 ms for its job, nobody cares. I have to include my own 
utility function there, at the same time knowing that every other programmer 
has such utility functions because the libraries don't have them. Uhhh...

Juha Manninen

More information about the fpc-pascal mailing list