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

发布于 2024-10-21 21:56:28 字数 177 浏览 5 评论 0原文

我正在使用 Delphi 2007,想知道是否有一种简单的方法来计算一个字符串在另一个字符串中出现的次数。我可以使用任何内置函数吗?

示例:

  • “How”在字符串“How are you?”中出现一次。
  • “do”在字符串“How do you do?”中出现两次

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?"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

橙幽之幻 2024-10-28 21:56:28
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;
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;
墟烟 2024-10-28 21:56:28

我见过的最聪明的方法之一:

{ 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 }

One of the most clever ways I've ever seen to do this:

{ 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 }
ˉ厌 2024-10-28 21:56:28

如果您发现自己经常在大量文本中搜索出现的内容并且性能成为问题,您可以尝试Boyer-Moore 搜索算法

查找所有出现的最坏情况
一篇文本大约需要 3n
比较

Delphi 中的实现可以在我们自己的 SO 此处

我需要三个快速大弦
功能:快速搜索、快速搜索
并替换,并快速计数
字符串中的子字符串。

If you find yourself frequently searching occurences in a large body of text and performance becomes an issue, you could try the Boyer-Moore search algorithm.

the worst-case to find all occurrences
in a text needs approximately 3n
comparisons

An implementation in Delphi can be found at our very own SO here

I need three fast-on-large-strings
functions: fast search, fast search
and replace, and fast count of
substrings in a string.

仲春光 2024-10-28 21:56:28

较新的 Delphi 版本有一个 CountChar< /a> 字符串助手,计算字符串中单个字符的出现次数:

var
  MyText: String;
begin
  MyText := 'How are you?';
  ShowMessage(MyText.CountChar('o').ToString);
end;

Newer Delphi versions have a CountChar string helper, counting occurrences of a single character in a string:

var
  MyText: String;
begin
  MyText := 'How are you?';
  ShowMessage(MyText.CountChar('o').ToString);
end;
韶华倾负 2024-10-28 21:56:28
function stringcount(pBefore: String; pSubstring: String; pFlags: TReplaceFlags): Integer;
begin
  result:= round((pBefore.Length - stringreplace(pBefore, pSubstring, '', pFlags).Length) / pSubstring.Length);
end;
function stringcount(pBefore: String; pSubstring: String; pFlags: TReplaceFlags): Integer;
begin
  result:= round((pBefore.Length - stringreplace(pBefore, pSubstring, '', pFlags).Length) / pSubstring.Length);
end;
月光色 2024-10-28 21:56:28

uses
  StrUtils;    

function Occurrences(const Substring, Text: string;
  const ignoreUppercase: Boolean = false): Integer;
var
  inSubstring, inText: string;
  inPos: Integer;
begin
  Result:= 0;

  if (Substring = '') or (Text = '') then
    Exit;

  if ignoreUppercase then
  begin
    inSubstring:= AnsiLowerCase(Substring);
    inText:=  AnsiLowerCase(Text);
  end
  else
  begin
    inSubstring:= Substring;
    inText:=  Text;
  end;

  inPos:= 1;

  repeat
    inPos:= posEx(inSubstring, inText, inPos);
    if inPos > 0 then
    begin
      Inc(Result);
      inPos:= inPos + Length(inSubstring);
    end;
  until inPos = 0;
end;


uses
  StrUtils;    

function Occurrences(const Substring, Text: string;
  const ignoreUppercase: Boolean = false): Integer;
var
  inSubstring, inText: string;
  inPos: Integer;
begin
  Result:= 0;

  if (Substring = '') or (Text = '') then
    Exit;

  if ignoreUppercase then
  begin
    inSubstring:= AnsiLowerCase(Substring);
    inText:=  AnsiLowerCase(Text);
  end
  else
  begin
    inSubstring:= Substring;
    inText:=  Text;
  end;

  inPos:= 1;

  repeat
    inPos:= posEx(inSubstring, inText, inPos);
    if inPos > 0 then
    begin
      Inc(Result);
      inPos:= inPos + Length(inSubstring);
    end;
  until inPos = 0;
end;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文