DelphiDabbler Code Snippets Database

Snippet Selection

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

ArrayToStringList

Copies the elements of string array Strings to string list SL, replacing any existing contents of SL.

procedure ArrayToStringList(const Strings: array of string;
  const SL: Classes.TStrings);
var
  Idx: Integer; // loops thru each string in array
begin
  SL.Clear;
  for Idx := 0 to Pred(Length(Strings)) do
    SL.Add(Strings[Idx]);
end;

FreeStringsObjects

Frees all objects stored in the Objects[] property of the given string list.

procedure FreeStringsObjects(const Strings: Classes.TStrings);
var
  Idx: Integer;
  Temp: TObject;
begin
  for Idx := 0 to Pred(Strings.Count) do
  begin
    Temp := Strings.Objects[Idx];
    Strings.Objects[Idx] := nil;
    Temp.Free;
  end;
end;

JoinStrArray

Joins all strings in string array Strings together into a single string, which is returned. Strings are separated by delimiter text Delim. Empty strings are ignored unless AllowEmpty is True, when they cause a delimiter to be appended to the result string.

function JoinStrArray(const Strings: array of string; const Delim: string;
  const AllowEmpty: Boolean = True): string;
var
  Idx: Integer; // loops thru all elements of string array
begin
  Result := '';
  for Idx := 0 to Pred(Length(Strings)) do
  begin
    if (Strings[Idx] <> '') or AllowEmpty then
      if Result = '' then
        Result := Strings[Idx]
      else
        Result := Result + Delim + Strings[Idx];
  end;
end;

StripWhiteSpace

Removes all white space from the given string and returns the result.

function StripWhiteSpace(const S: string): string;
var
  Idx: Integer;       // loops thru all characters in string
  ResCount: Integer;  // counts number of characters in result string
  PRes: PChar;        // pointer to characters in result string
begin
  SetLength(Result, Length(S));
  PRes := PChar(Result);
  ResCount := 0;
  Idx := 1;
  while Idx <= Length(S) do
  begin
    if (SysUtils.ByteType(S, Idx) <> mbSingleByte)
      or not IsWhiteSpace(S[Idx]) then
    begin
      PRes^ := S[Idx];
      Inc(ResCount);
      Inc(PRes);
    end;
    Inc(Idx);
  end;
  if ResCount < Length(S) then
    SetLength(Result, ResCount);
end;

StrToken

Finds and returns the first token in string S, delimited by the given separator character. The token and separator are removed from S. If no separators are found then the whole of S is returned and S is then set to the empty string.

function StrToken(var S: string; Separator: Char): string;
var
  Idx: Cardinal;  // index of Separator in S
begin
  Idx := SysUtils.AnsiPos(Separator, S);
  if Idx > 0 then
  begin
    Result := System.Copy(S, 1, Idx - 1);
    System.Delete(S, 1, Idx);
  end
  else
  begin
    Result := S;
    S := '';
  end;
end;

StrTokenCount

Counts and returns the number of tokens in string S delimited by the given separator character. Empty strings before separators are counted as tokens, but an empty string after the last separator is not.

function StrTokenCount(S: string; Seperator: Char): Integer;
begin
  Result := 0;
  while S <> '' do
  begin
    StrToken(S, Seperator);
    Inc(Result);
  end;
end;

View the whole database.

Go to the DelphiDabbler website.