33

Does there exist any RTL Delphi function to determine the position of the last occurrence of a char in a string?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
DelphiNewbie
  • 769
  • 3
  • 9
  • 14

5 Answers5

35

try the LastDelimiter function which is part of the SysUtils unit.

dummzeuch
  • 10,975
  • 4
  • 51
  • 158
RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • 2
    System.SysUtils.TStringHelper.LastIndexOf could also be handy when you want a string instead of a character. – Gabriel Apr 07 '17 at 08:59
22

RRUZ answered the actual question (he gave you a RTL function).

Still, I cannot quite resist giving a simple code snippet that does what you want:

function LastCharPos(const S: string; const Chr: char): integer;
var
  i: Integer;
begin
  result := 0;
  for i := length(S) downto 1 do
    if S[i] = Chr then
      Exit(i);
end;

Since this does exactly what you want and offer no other features, it is far more compact (especially when we use the Exit(Result) syntax of Delphi 2009 and later) and probably slightly faster. In Delphi 2007, however, you have to do

function LastCharPos(const S: string; const Chr: char): integer;
var
  i: Integer;
begin
  result := 0;
  for i := length(S) downto 1 do
    if S[i] = Chr then
    begin
      result := i;
      break; // or Exit; if you prefer that
    end;
end;
Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
12

Use StrRScan or AnsiStrRScan, both in the SysUtils unit. The latter, despite its name, works on Unicode characters in the Delphi versions where string is UnicodeString. (If you still need the "real" Ansi version, use the AnsiStrings unit.)

These functions search for exactly one character, whereas LastDelimiter searches for any of several characters from the given list of possibilities — think of StrRScan as LastDelimiter optimized for a one-character Delimiters argument.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
4

The best cross-platform solution is TStringHelper.LastIndexOf, it exists since Delphi XE4.

Note, that this function is 0-based.

LU RD
  • 34,438
  • 5
  • 88
  • 296
vladon
  • 8,158
  • 2
  • 47
  • 91
1

And here's my contribution for finding the position of the nth occurrence of a substring within a string.

function GetPositionOfNthOccurence(sSubStr, sStr: string; iNth: integer): integer;
var
  sTempStr: string;
  iIteration: integer;
  iTempPos: integer;
  iTempResult: integer;
begin
  result := 0;

  // validate input parameters
  if ((iNth < 1) or (sSubStr = '') or (sStr = '')) then exit;

  // evaluate
  iIteration := 0;
  iTempResult := 0;
  sTempStr := sStr;
  while (iIteration < iNth) do
  begin
    iTempPos := Pos(sSubStr, sTempStr);
    if (iTempPos = 0) then exit;
    iTempResult := iTempResult + iTempPos;
    sTempStr := Copy(sStr, iTempResult + 1, Length(sStr) - iTempResult);
    inc(iIteration);
  end;
  result := iTempResult;
end;
Sam
  • 2,663
  • 10
  • 41
  • 60
  • very good, because with this code also is possible determine quantity of occurences of a substring in a string. This solved a trouble to me now! thank you by your contribution. –  Oct 05 '17 at 18:32