Pascal 'Split' Function

20,378

Solution 1

With a TStringListdo as follows:

procedure SplitText(aDelimiter: Char; const s: String; aList: TStringList);
begin
  aList.Delimiter := aDelimiter;
  aList.StrictDelimiter := True; // Spaces excluded from being a delimiter
  aList.DelimitedText := s;
end;

Note: The StrictDelimiter property was added in D2006.

Another way:

procedure SplitText(const aDelimiter,s: String; aList: TStringList);
begin
  aList.LineBreak := aDelimiter;
  aList.Text := s;
end;

Can use multiple characters as a delimiter.

Solution 2

The Delphi RTL already has the precise function that you need, SplitString from the System.StrUtils unit:

function SplitString(const S, Delimiters: string): TStringDynArray;

Documented as:

Splits a string into different parts delimited by the specified delimiter characters.

SplitString splits a string into different parts delimited by the specified delimiter characters. S is the string to be split. Delimiters is a string containing the characters defined as delimiters.

SplitString returns an array of strings of type System.Types.TStringDynArray that contains the split parts of the original string.

Solution 3

Well, everyone posts their traditional answers here, so will do i.

I see 2 answers already posted, but i don't know if the fourth-one (PChar-based ExtractStrings) would be, before this dupe will be closed.

Overall this is a duplicate of Split a string into an array of strings based on a delimiter and all the answers can be seen there.

http://jcl.sf.net http://wiki.delphi-jedi.org/wiki/JCL_Help:IJclStringList

var OutPutVariable: iJclStringList; 

OutPutVariable := JclStringList().Split('Word1.Word2.Word3','.');

Now

{ OutPutVariable[0] would be 'Word1'}
{ OutPutVariable[1] would be 'Word2'}
{ OutPutVariable[2] would be 'Word3'}

If you insist on your original indexing

{ OutPutVariable[1] would be Word1}
{ OutPutVariable[2] would be Word2}
{ OutPutVariable[3] would be Word3}

Then add a stub 0th string

OutPutVariable := JclStringList().Split('.Word1.Word2.Word3','.');

or

OutPutVariable := JclStringList().Add('').Split('Word1.Word2.Word3','.', False);

It also provides for Join and many other functions.

PS: 4th variant is http://docwiki.embarcadero.com/Libraries/XE2/en/System.Classes.ExtractStrings

Share:
20,378
Admin
Author by

Admin

Updated on November 15, 2020

Comments

  • Admin
    Admin almost 3 years

    I am coding a little program in pascal and I have run into a small problem. In other languages there is a function named 'split' or 'explode' to take a long string that is punctuated by a defined character and splits this long string into several smaller strings and assigns them to an array. Here is what I mean, I would like to do this:

    longstring:='Word1.Word2.Word3');
    
    Split('.', longstring, OutPutVariable) ;
    
    { OutPutVariable[1] would be Word1}
    { OutPutVariable[2] would be Word2}
    { OutPutVariable[3] would be Word3}
    

    This is not real code, as the 'split' does not exist in pascal. I think it exists in Delphi though. Can anypne help me with this problem? Sorry if it is a really easy problem, I am new to programming

  • JensG
    JensG almost 10 years
    +1 similar idea, but your implementation is better. Except that it doesn't compile and I would add an ASSERT(list<> nil); at the beginning.
  • Conrad Hildebrand
    Conrad Hildebrand almost 10 years
    You should probably add that this is only available in Delphi XE and higher.
  • David Heffernan
    David Heffernan almost 10 years
    @jpfollenius I did not actually know that. But XE is ancient now.
  • Gerry Coll
    Gerry Coll almost 10 years
    Note that DelimitedText includes handling of QuoteChar, so it doesn't strictly match .net's String.Split
  • LU RD
    LU RD almost 10 years
    @GerryColl, correct. Setting list.QuoteChar := #0; would eliminate this possibility, or just use the LineBreak procedure.
  • Uwe Raabe
    Uwe Raabe almost 10 years
    @jpfollenius, if nothing else is mentioned in the tags or the question, I always assume the Delphi version that is current at the time the question is posted here. If anyone wants specific answers, he should be more precise asking his question. Furthermore, that answer can be helpful for others even if not for the OP.
  • Conrad Hildebrand
    Conrad Hildebrand almost 10 years
    @UweRaabe sure it is! I did not criticize the answer (upvoted indeed) but just suggested a small edit.
  • Arioch 'The
    Arioch 'The almost 10 years
    @jpfollenius for pre-XE Delphi there is another function: docwiki.embarcadero.com/Libraries/XE2/en/…