Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2009-11

Split and Join

6 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Juha Manninen

Hi,

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;

and

  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 on.

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...

Regards,
Juha Manninen

Reply by email

Juha Manninen
to Juha Manninen

Hi,

I can actually use TStringList's Delimiter and DelimitedText for basic splitting. This separates words of a text:

  List.Delimiter := ' ';
  List.DelimitedText := S;

I have programmed with Delphi for years without knowing it. Now I found it with a google search. Should have waited a little before sending the mail... Anyway, a more optimized version of Split and a Join are still missing from library.

Juha Manninen

Reply by email

Michael Van Canneyt
to Juha Manninen: FPC's strutils or other libraries still don't have functions for splitting a string to a…
Juha Manninen wrote 9 lines
Hi, 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; and function Join(List: TStringList; Delim: string): string;

Of course it does exist. Split can be implemented like this:

List:=TStringList.Create;
List.Delimiter:=Delim;
List.StrictDelimiters:=True;
List.DelimitedText:=Str;

And that's it.

Michael.

Reply by email

Mattias Gaertner
to Michael Van Canneyt
Show quoted text 22 lines
On Fri, 27 Nov 2009, Juha Manninen wrote: > Hi, > > 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; > and > function Join(List: TStringList; Delim: string): string; Of course it does exist. Split can be implemented like this: List:=TStringList.Create; List.Delimiter:=Delim; List.StrictDelimiters:=True; List.DelimitedText:=Str; And that's it.

Many other languages can do that in one line with their RTL.

Maybe as a special constructor.

List:=TStringList.CreateFromSplit(SomeText,Delim);

Mattias

Reply by email

Michael Van Canneyt
to Mattias Gaertner
Mattias Gaertner wrote 31 lines
On Fri, 27 Nov 2009 20:11:25 +0100 (CET) Michael Van Canneyt wrote: > > > On Fri, 27 Nov 2009, Juha Manninen wrote: > >> Hi, >> >> 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; >> and >> function Join(List: TStringList; Delim: string): string; > > Of course it does exist. Split can be implemented like this: > > List:=TStringList.Create; > List.Delimiter:=Delim; > List.StrictDelimiters:=True; > List.DelimitedText:=Str; > > And that's it. Many other languages can do that in one line with their RTL. Maybe as a special constructor. List:=TStringList.CreateFromSplit(SomeText,Delim);

That looks like a good suggestion, I'll add it as TStringList.CreateSplit.

Michael.

Reply by email

Luca Olivetti
to Michael Van Canneyt: Of course it does exist. Split can be implemented like this: List:=TStringList.Create;…

En/na Michael Van Canneyt ha escrit:

Show quoted text 7 lines
Of course it does exist. Split can be implemented like this: List:=TStringList.Create; List.Delimiter:=Delim; List.StrictDelimiters:=True; List.DelimitedText:=Str;

Never though of it. And join can be done too.
Pity that delimiter is a single character.
e.g., in python I can do

>> l=('one','two','three') >> ', '.join(l)

'one, two, three'

>> ', '.join(l).split(', ')

['one', 'two', 'three']

it's handy nevertheless.

Bye

Luca

Reply by email