5

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

7

4 Answers 4

9

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.

Sign up to request clarification or add additional context in comments.

5 Comments

You should probably add that this is only available in Delphi XE and higher.
@jpfollenius I did not actually know that. But XE is ancient now.
@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.
@UweRaabe sure it is! I did not criticize the answer (upvoted indeed) but just suggested a small edit.
@jpfollenius for pre-XE Delphi there is another function: docwiki.embarcadero.com/Libraries/XE2/en/…
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.

3 Comments

+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.
Note that DelimitedText includes handling of QuoteChar, so it doesn't strictly match .net's String.Split
@GerryColl, correct. Setting list.QuoteChar := #0; would eliminate this possibility, or just use the LineBreak procedure.
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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.