string 查找字符串中最后一次出现的字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5844406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find the last occurrence of char in a string
提问by DelphiNewbie
Does there exist any RTL Delphi function to determine the position of the last occurrence of a char in a string?
是否存在任何 RTL Delphi 函数来确定字符串中最后一次出现字符的位置?
回答by RRUZ
try the LastDelimiter
function which is part of the SysUtils unit.
尝试作为LastDelimiter
SysUtils 单元一部分的功能。
回答by Andreas Rejbrand
RRUZ answered the actual question (he gave you a RTL function).
RRUZ 回答了实际问题(他给了你一个 RTL 函数)。
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
由于这完全符合您的要求并且不提供其他功能,因此它更加紧凑(尤其是当我们使用Exit(Result)
Delphi 2009 及更高版本的语法时)并且可能会稍微快一些。但是,在 Delphi 2007 中,您必须执行
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;
回答by Rob Kennedy
Use StrRScan
or AnsiStrRScan
, both in the SysUtilsunit. 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 AnsiStringsunit.)
在SysUtils单元中使用StrRScan
或。后者,尽管它的名字,适用于在Delphi版本,其中Unicode字符是。(如果您仍然需要“真正的” Ansi 版本,请使用AnsiStrings单元。)AnsiStrRScan
string
UnicodeString
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.
这些函数只搜索一个字符,而LastDelimiter
从给定的可能性列表中搜索多个字符中的任何一个——可以认为StrRScan
是LastDelimiter
针对单字符Delimiters
参数进行了优化。
回答by vladon
The best cross-platform solution is TStringHelper.LastIndexOf, it exists since Delphi XE4.
最好的跨平台解决方案是TStringHelper.LastIndexOf,它自 Delphi XE4 以来就存在。
Note, that this function is 0-based.
请注意,此函数是基于 0 的。
回答by Sam
And here's my contribution for finding the position of the nth occurrence of a substring within a string.
这是我在查找字符串中子字符串第 n 次出现位置的贡献。
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;