'Pascal 'Split' Function

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



Solution 1:[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:[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:[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

Solution 4:[4]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 David Heffernan
Solution 3 Community
Solution 4 ??? ???? ????