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整理六(数据与记录)

Delphi 2009 之 tstringbuilder 类[6]: equals

Delphi 提升程序进程权限为debug权限

Delphi cannot find implementation of method formcreate 解决办法

Delphi 一个call应该如何写?

Delphi fastreport快速入门

Delphi 检查ip地址合法性

Delphi extctrls.frame3d

Delphi 取当前目录下所有文件夹名和文件名

fmsoft_unigui个文件说明

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



打赏

取消

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

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

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

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

评论

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