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
With a TStringList
do 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With