Delphi:计数字符串在另一个字符串中的次数

前端之家收集整理的这篇文章主要介绍了Delphi:计数字符串在另一个字符串中的次数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用Delphi 2007,并想知道是否有一种简单的方法来计算字符串在另一个字符串中发生的次数。任何内置函数我可以使用?

例子:

>“如何”一次出现在字符串“你好吗?
>“do”在字符串中出现两次“你好吗?”

解决方法

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,offset + length(Substring));
  end;
end;
原文链接:https://www.f2er.com/delphi/103387.html

猜你在找的Delphi相关文章