user1556433
user1556433

Reputation:

How to count number of occurrences of a certain char in string?

How can I count the number of occurrences of a certain character in a string in Delphi?

For instance, assume that I have the following string and would like to count the number of commas in it:

S := '1,2,3';

Then I would like to obtain 2 as the result.

Upvotes: 26

Views: 41088

Answers (9)

Roland Bengtsson
Roland Bengtsson

Reputation: 5148

Why not use built in method ? I prefer that if it exists. https://docwiki.embarcadero.com/Libraries/Sydney/en/System.SysUtils.TStringHelper.CountChar

Upvotes: 1

Katte
Katte

Reputation: 1

I was missing a recursive solution like that:

function CountOccurences(const SubText, Text: string): Integer;
var
  PosRes: Integer;
begin
  // Find the position of SubText in Text
  PosRes := Pos(SubText, Text);

  // If SubText is not found, return 0
  if PosRes = 0 then
    Result := 0
  else
    // If SubText is found, add 1 and call CountOccurences recursively 
    // for the remaining string after the found occurrence
    Result := 1 + CountOccurences(SubText, Copy(Text, PosRes + Length(SubText), MaxInt));
end;

Upvotes: 0

wywtk-com
wywtk-com

Reputation: 1

Ummm... Am I missing something? Why not just...

      kSepChar:=',';//to count commas
      bLen:=length(sLineToCheck);
      bCount:=0;//The numer of kSepChars seen so far.
      bPosn:=1;//First character in string is at position 1
      for bPosn:=1 to bLen do begin
         if sLineToCheck[bPosn]=kSepChar then inc(bCount);
         end;//

Upvotes: 0

Alieh S
Alieh S

Reputation: 170

Simple solution and good performance (I wrote for Delphi 7, but should work for other versions as well):

function CountOccurences(const ASubString: string; const AString: string): Integer;
var
  iOffset: Integer;
  iSubStrLen: Integer;
begin
  Result := 0;

  if (ASubString = '') or (AString = '') then
    Exit;

  iOffset := 1;
  iSubStrLen := Length(ASubString);
  while (True) do
  begin
    iOffset := PosEx(ASubString, AString, iOffset);
    if (iOffset = 0) then
      Break;
    Inc(Result);
    Inc(iOffset, iSubStrLen);
  end;
end;

Upvotes: 1

Marwan Almukh
Marwan Almukh

Reputation: 201

You can use the benefit of StringReplace function as:

function OccurencesOfChar(ContentString:string; CharToCount:char):integer;
begin
   Result:= Length(ContentString)-Length(StringReplace(ContentString, CharToCount,'', [rfReplaceAll, rfIgnoreCase]));
end;

Upvotes: 2

RobertFrank
RobertFrank

Reputation: 7384

Even though an answer has already been accepted, I'm posting the more general function below because I find it so elegant. This solution is for counting the occurrences of a string rather than a character.

{ Returns a count of the number of occurences of SubText in Text }
function CountOccurences( const SubText: string;
                          const Text: string): Integer;
begin
  Result := Pos(SubText, Text); 
  if Result > 0 then
    Result := (Length(Text) - Length(StringReplace(Text, SubText, '', [rfReplaceAll]))) div  Length(subtext);
end;  { CountOccurences }

Upvotes: 20

Raul
Raul

Reputation: 666

This one can do the work for if you're not handling large text

...

uses RegularExpressions;

...

function CountChar(const s: string; const c: char): integer;
begin
 Result:= TRegEx.Matches(s, c).Count
end;

Upvotes: 12

Ken White
Ken White

Reputation: 125620

And for those who prefer the enumerator loop in modern Delphi versions (not any better than the accepted solution by Andreas, just an alternative solution):

function OccurrencesOfChar(const ContentString: string;
  const CharToCount: char): integer;
var
  C: Char;
begin
  result := 0;
  for C in ContentString do
    if C = CharToCount then
      Inc(result);
end;

Upvotes: 18

Andreas Rejbrand
Andreas Rejbrand

Reputation: 108919

You can use this simple function:

function OccurrencesOfChar(const S: string; const C: char): integer;
var
  i: Integer;
begin
  result := 0;
  for i := 1 to Length(S) do
    if S[i] = C then
      inc(result);
end;

Upvotes: 43

Related Questions