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 get_hd_serial() 获得磁盘驱动器序列号

Delphi 批量日期格式化

Delphi inc () 计数用法

Delphi dbgrid适应宽度

汇编数据宽度和字节序

Delphi 锁住listview防止刷新

线程与进程的区别

Delphi 提升进程令牌

Delphi2010中字符串汇编需要注意的一点,以及支持2010的aes加密库

Delphi 关于 "高位" 与 "低位"

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



打赏

取消

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

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

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

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

评论

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