频道分类

Delphi 计算一个字符串在另一个字符串中出现的次数

作者:admin 来源: 日期:2020/6/25 9:37:01 人气: 标签:

 
function Occurrences(const Substring, Text: string): integer;
var
  offset: integer;
begin
  result := 0;
  offset := PosEx(Substring, Text, 1);
  while offset <> 0 do
  begin
    inc(result);
    offset := PosEx(Substring, Text, offset + length(Substring));
  end;
end;

{ Returns a count of the number of occurences of SubText in Text }
function CountOccurences( const SubText: string;
                          const Text: string): Integer;
begin
  if (SubText = '') OR (Text = '') OR (Pos(SubText, Text) = 0) then
    Result := 0
  else
    Result := (Length(Text) - Length(StringReplace(Text, SubText, '', [rfReplaceAll]))) div  Length(subtext);
end;  { CountOccurences }


来源:https://cloud.tencent.com/developer/ask/217083
https://stackoverflow.com/questions/5265317/delphi-count-number-of-times-a-string-occurs-in-another-string

上一篇:Delphi 提取标签之间的字符串下一篇:没有资料