Delphi: count number of times a string occurs in another string

前端 未结 5 1235
野性不改
野性不改 2020-12-10 10:49

I\'m using Delphi 2007 and wonder if there is a simple way of counting the number of times a string occurs in another string. Any builtin function I can use?

Example

5条回答
  •  天涯浪人
    2020-12-10 11:40

    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;
    

提交回复
热议问题