Delphi:计数字符串在另一个字符串中的次数 [英] Delphi: count number of times a string occurs in another string

查看:244
本文介绍了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,Text,offset + length(Substring));
结束
结束


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?

Examples:

  • "How" occurs once in the string "How are you?"
  • "do" occurs twice in the string "How do you 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, Text, offset + length(Substring));
  end;
end;

这篇关于Delphi:计数字符串在另一个字符串中的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆