Skip to content

Instantly share code, notes, and snippets.

@Drarok
Created March 6, 2011 23:16
Show Gist options
  • Save Drarok/857859 to your computer and use it in GitHub Desktop.
Save Drarok/857859 to your computer and use it in GitHub Desktop.
Function Explode(Separator, S : String) : TStringList;
Var
PrevPos, x : Integer;
strLen,
delimLen : Integer;
Begin
If (Separator = '') Then
Raise Exception.Create('Empty separator');
Result := TStringList.Create();
If (S = '') Then
Exit;
PrevPos := 1;
strLen := Length(S);
delimLen := Length(Separator);
For x := 1 To strLen + 1 Do
Begin
// Reached the end of the string || Found a separator.
If (x = (strLen + 1)) Or (Copy(S, x, delimLen) = Separator) Then
Begin
Result.Add(Copy(S, PrevPos, x - PrevPos));
PrevPos := x + delimLen;
End; {If}
End;
End;
Function Implode( Glue : String; Parts : TStringList) : String;
Var
x : Integer;
Begin
Result := '';
For x := 0 To Parts.Count - 1 Do
Begin
Result := Result + Parts.Strings[x] + Glue;
End; {For}
Result := Copy(Result, 1, Length(Result) - Length(Glue)); // Trim off trailing glue.
End;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment