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


本文整理自网络,侵删。

 
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 取 utc/tfiletime 时间

Delphi 关于运算符

Delphi tstringdynarray 使用,分割字符串

Delphi ip编辑控件

Delphi动态设置spcomm的属性值

Delphi 计算文件与当前时间差(天小时分钟秒)

Delphi开发firemonkey应用程序时无法打开窗体的解决

Delphi中setlength使用

Delphi 调用外部程序并等待其运行结束

Delphi copyrect实现的几个图片的转换效果

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...