DelphiDabbler Code Snippets Database

Snippet Selection

The following snippets from the Code Snippets Database have been requested.

ChangeChar

Replaces all occurrences the character ASearch in AString with the AReplace character, and returns the resulting string.

function ChangeChar(const AString: string; ASearch, AReplace: Char): string;
var
  I: integer; // loops thru all chars of string
begin
  Result := AString;
  if Result = '' then
    Exit;
  for I := 1 to Length(Result) do
    if Result[I] = ASearch then
      Result[I] := AReplace;
end;

ExplodeStr

Splits the string S into segments separated by delimiter Delim and stores each segment in turn in string-list List, replacing any existing content. If Trim is True segments are trimmed of leading and trailing spaces. Empty segments (after any trimming) are discarded if AllowEmpty is False. The number of segments added to List is returned.

function ExplodeStr(S, Delim: string; const List: Classes.TStrings;
  const AllowEmpty: Boolean = True; const Trim: Boolean = False): Integer;
var
  Item: string;       // current delimited text
  Remainder: string;  // remaining un-consumed part of string

  // ---------------------------------------------------------------------------
  procedure AddItem;
  begin
    // Adds optionally trimmed item to list if required
    if (Trim) then
      Item := SysUtils.Trim(Item);
    if (Item <> '') or AllowEmpty then
      List.Add(Item);
  end;
  // ---------------------------------------------------------------------------

begin
  // Clear the list
  List.Clear;
  // Check we have some entries in the string
  if S <> '' then
  begin
    // Repeatedly split string until we have no more entries
    while SplitStr(S, Delim, Item, Remainder) do
    begin
      AddItem;
      S := Remainder;
    end;
    // Add any remaining item
    AddItem;
  end;
  Result := List.Count;
end;

ExplodeStrArray

Splits the string S into segments separated by delimiter Delim and returns a string array containing the segments. If Trim is True segments are trimmed of leading and trailing spaces. Empty segments (after any trimming) are discarded if AllowEmpty is False.

function ExplodeStrArray(const S, Delim: string;
  const AllowEmpty: Boolean = True;
  const Trim: Boolean = False): Types.TStringDynArray;
var
  Strings: Classes.TStringList; // intermediate string list for ExplodeStr
begin
  Strings := Classes.TStringList.Create;
  try
    ExplodeStr(S, Delim, Strings, AllowEmpty, Trim);
    Result := StringListToArray(Strings);
  finally
    Strings.Free;
  end;
end;

ReduceStr

Reduces the length of string S by the number of characters given by ReduceBy and returns the result.

function ReduceStr(const Str: string; ReduceBy: Integer): string;
begin
  Result := Copy(Str, 1, Length(Str) - ReduceBy);
end;

SplitStr

Splits string S at the first occurence of delimiter string Delim. S1 is set to to the sub-string of S before Delim and S2 to the remainder of S after Delim. If Delim is not found then S1 is set to S and S2 to the empty string. Returns True if S contains Delim or False if not.

function SplitStr(const S, Delim: string; out S1, S2: string): Boolean;
var
  DelimPos: Integer;  // position of delimiter in source string
begin
  // Find position of first occurence of delimiter in string
  DelimPos := SysUtils.AnsiPos(Delim, S);
  if DelimPos > 0 then
  begin
    // Delimiter found: split and return True
    S1 := Copy(S, 1, DelimPos - 1);
    S2 := Copy(S, DelimPos + Length(Delim), MaxInt);
    Result := True;
  end
  else
  begin
    // Delimiter not found: return false and set S1 to whole string
    S1 := S;
    S2 := '';
    Result := False;
  end;
end;

StringListToArray

Creates and returns a dynamic string array containing all the strings from the given string list.

function StringListToArray(const SL: Classes.TStrings): Types.TStringDynArray;
var
  Idx: Integer; // loops thru each string in SL
begin
  SetLength(Result, SL.Count);
  for Idx := 0 to Pred(SL.Count) do
    Result[Idx] := SL[Idx];
end;

View the whole database.

Go to the DelphiDabbler website.