DelphiDabbler Code Snippets Database

Snippet Selection

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

CutWordByIndex

Deletes the Nth word from string Str and returns the deleted word. Words are delimited by any of the characters in Delims. If there is no Nth word then the empty string is returned and Str is not modified.

function CutWordByIndex(const N: Integer; var Str: string;
  const Delims: TCharSet): string;
var
  St, En: Integer;  // start and end of string to cut
begin
  if GetStartAndEndWord(N, Str, Delims, St, En) then
  begin
    Result := Copy(Str, St, En - St + 1);
    Delete(Str, St, En - St + 1);
  end;
end;

DeleteWordByIndex

Deletes and discards the Nth word from string Str. Words are delimited by any of the characters in Delims. Returns True if the Nth word was found and deleted from Str or False if there is no Nth word and Str is unchanged.

function DeleteWordByIndex(const N: Integer; var Str: string;
  const Delims: TCharSet): Boolean;
var
  St, En: Integer; // start and end of string to delete
begin
  Result := GetStartAndEndWord(N, Str, Delims, St, En);
  if Result then
    Delete(Str, St, En - St + 1);
end;

GetStartAndEndWord

Finds the location of the Nth word in string Str and passes the start and end of the location out in StartIdx and EndIdx. Words are delimited by the any of the characters in Delim. Returns True if the Nth word was found or False, with StartIdx set to 0, if not.

function GetStartAndEndWord(const N: Integer; const Str: string;
  const Delims: TCharSet; out StartIdx, EndIdx: Integer): Boolean;
var
  I, J: Integer;  // loops thru string finding start and end of word
  Count: Integer; // index of found word in string
  SLen: Integer;  // length of string
begin
  Result := False;
  Count := 0;
  I := 1;
  StartIdx := 0;
  EndIdx := 0;
  SLen := Length(Str);
  while I <= SLen do
  begin
    while (I <= SLen) and (IsCharInSet(Str[I], Delims)) do
      Inc(I);
    if I <= SLen then
      Inc(Count);
    J := I;
    while (J <= SLen) and not (IsCharInSet(Str[J], Delims)) do
      Inc( J );
    if Count = N then
    begin
      StartIdx := I;
      EndIdx := J - 1;
      Result := StartIdx > 0;
      Exit;
    end;
    I := J;
  end;
end;

GetWordByIndex

Locates and returns the Nth word from string Str. Words are delimited by the any of the characters in Delim. If there is no Nth word then the empty string is returned.

function GetWordByIndex(const N: Integer; const Str: string;
  const Delims: TCharSet): string;
var
  St, En: Integer;  // start and end of string to find
begin
  if GetStartAndEndWord(N, Str, Delims, St, En) then
    Result := Copy(Str, St, En - St + 1)
  else
    Result := '';
end;

PosByIndex

Returns the start position of the Nth word in string Str. Words are delimited by any of the characters in Delim. Returns 0 if there is no such word.

function PosByIndex(const N: Integer; const Str: string;
  const Delims: TCharSet): Integer;
var
  En: integer; // index of end of word in AString: not used
begin
  GetStartAndEndWord(N, Str, Delims, Result, En);
end;

ReplaceWordByIndex

Replaces the Nth word from string Str with NewWord. Words are delimited by the any of the characters in Delim. Modifies Str and returns True on success. Returns False and leaves Str unchanged if there is no Nth word.

function ReplaceWordByIndex(const N: Integer; const NewWord: string;
  var Str: string; const Delims: TCharSet): Boolean;
var
  St, En: Integer;  // start and end of word to change
begin
  Result := GetStartAndEndWord(N, Str, Delims, St, En);
  if Result then
  begin
    Delete(Str, St, En - St + 1);
    Insert(NewWord, Str, St);
  end;
end;

View the whole database.

Go to the DelphiDabbler website.