Count Char Occurrences


#1

I want to share a code snippet. Often you need to count a specific occurrence of a character. This is what I found:

Units needed: SysUtils
Method: TStringHelper.CountChar()

Example

program CountCharOccurrences;
uses SysUtils;
var
  s: String;
  c: Char;
begin
  s := '1k1k1k1k1';
  c := '1';
  WriteLn(s.CountChar(c));   // Returns 5
  WriteLn(s.CountChar(s[2]));   // Returns 4
end.

Source: http://docwiki.appmethod.com/appmethod/1.16/libraries/en/System.SysUtils.TStringHelper.CountChar