Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 987
user2832521 Avatar asked Dec 05 '22 08:12

user2832521


1 Answers

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.

like image 91
LU RD Avatar answered Dec 28 '22 07:12

LU RD