In Delphi, you can use the StringReplace function from the System unit to implement the implode function, which concatenates the elements of an array into a string.
Here is an example of how you can implement the implode function in Delphi:
1 2 3 4 5 6 7 8 9 10 11 12 |
function Implode(const Delimiter string; const Values array of string) string; var I Integer; begin Result = ”; for I = Low(Values) to High(Values) do begin if I Low(Values) then Result = Result + Delimiter; Result = Result + Values[I]; end; end; |
This function takes a delimiter string and an array of strings as arguments, and it returns a single string that is the concatenation of the array elements separated by the delimiter.
To use the Implode function, you can call it like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
uses System; var Str: string; Values: array[0..2] of string; begin Values[0] := ‘apple’; Values[1] := ‘banana’; Values[2] := ‘orange’; Str := Implode(‘, ‘, Values); WriteLn(Str); Output apple, banana, orange end. |
To implement the explode function, which splits a string into an array of strings based on a delimiter, you can use the SplitString function from the System.StrUtils unit.
Here is an example of how you can implement the explode function in Delphi:
1 2 3 4 |
function Explode(const Delimiter: string; const S: string) TStringDynArray; begin Result := SplitString(S, Delimiter); end; |
This function takes a delimiter string and a string as arguments, and it returns a dynamic array of strings that is the result of splitting the input string by the delimiter.
To use the Explode function, you can call it like this:
1 2 3 4 5 6 7 8 9 10 11 |
uses System.StrUtils; var Values: TStringDynArray; I: Integer; begin Values = Explode(‘, ‘, ‘apple, banana, orange’); for I = Low(Values) to High(Values) do WriteLn(Values[I]); end. |
Output
1 2 3 |
apple banana orange |
Leave a Reply