4

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

user2832521
  • 49
  • 1
  • 1
  • 2
  • You tagged with Delphi so if you are on Delphi XE or later you can use [SplitString](http://docwiki.embarcadero.com/Libraries/XE5/en/System.StrUtils.SplitString) – Mikael Eriksson Nov 14 '13 at 18:04
  • If not, this has been implemented several times here at SO. – Andreas Rejbrand Nov 14 '13 at 18:08
  • 1
    possible duplicate of [How to split a string of only ten characters e.g."12345\*45688" into an array](http://stackoverflow.com/questions/5928984/how-to-split-a-string-of-only-ten-characters-e-g-1234545688-into-an-array) – Ken White Nov 14 '13 at 18:11
  • @KenWhite, no that question is more towards an expression parser with multiple separators. – LU RD Nov 14 '13 at 18:18
  • @LURD: You're right. Wrong link: http://stackoverflow.com/q/7491036/62576 – Ken White Nov 14 '13 at 18:24
  • 2
    I'd say this is dupe of http://stackoverflow.com/questions/2625707 – Arioch 'The Nov 15 '13 at 10:05
  • possible duplicate of [Split a string into an array of strings based on a delimiter](http://stackoverflow.com/questions/2625707/split-a-string-into-an-array-of-strings-based-on-a-delimiter) – kobik Nov 15 '13 at 17:07

4 Answers4

8

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.

LU RD
  • 34,438
  • 5
  • 88
  • 296
  • +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. – JensG Nov 14 '13 at 18:13
  • Note that DelimitedText includes handling of QuoteChar, so it doesn't strictly match .net's `String.Split` – Gerry Coll Nov 14 '13 at 21:52
  • @GerryColl, correct. Setting `list.QuoteChar := #0;` would eliminate this possibility, or just use the `LineBreak` procedure. – LU RD Nov 14 '13 at 22:50
7

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    You should probably add that this is only available in Delphi XE and higher. – jpfollenius Nov 14 '13 at 21:28
  • @jpfollenius I did not actually know that. But XE is ancient now. – David Heffernan Nov 14 '13 at 21:30
  • 2
    @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. – Uwe Raabe Nov 14 '13 at 23:11
  • 2
    @UweRaabe sure it is! I did not criticize the answer (upvoted indeed) but just suggested a small edit. – jpfollenius Nov 15 '13 at 07:23
  • @jpfollenius for pre-XE Delphi there is another function: http://docwiki.embarcadero.com/Libraries/XE2/en/System.Classes.ExtractStrings – Arioch 'The Nov 15 '13 at 10:01
1

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

Community
  • 1
  • 1
Arioch 'The
  • 15,799
  • 35
  • 62
-1

hello use that code i belive it's good but don't forget to define strarray=array [0..9] of string; as type

enter image description here

unit Unit1;

interface

uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, 
Forms,   Dialogs, StdCtrls;

type   strarray=array [0..9] of string;

  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);   private
    { Private declarations }   public
    { Public declarations }   end;

var   Form1: TForm1;

implementation

{$R *.dfm}

function Split(deli:string;longstring:string):strarray; 
var rusarray:strarray; 
i,b,n:integer; 
begin  
n:=0; b:=1; 
for i:=0 to length(longstring) do
    if longstring[i]=deli then
       begin
       rusarray[n]:=copy(longstring,b,i-b);
       n:=n+1;
       b:=i+1;
       end;
      rusarray[n]:=copy(longstring,b,length(longstring)); result:=rusarray; end;

procedure TForm1.Button1Click(Sender: TObject); 
var  
rusarray:strarray; 
i:integer; 
longstring,Delimiter:string; 
begin 
longstring:='Word1-Word2-Word3'; 
Delimiter:='-'; 
rusarray:=Split(Delimiter, longstring) ; 
memo1.Clear ; 
for i:=0 to 10 do    
    if not ((rusarray[i]='')or(rusarray[i]=Delimiter)) then
     memo1.Lines.Add(rusarray[i]) 
end;

end.