2016/08/12

[整理]如何計算出一個字元,在一個字串中出現的次數

字元(Char)計數
1
2
3
4
5
6
7
8
9
function OccurrencesOfChar(const S: stringconst C: char): integer;
var
  i: Integer;
begin
  result := 0;
  for i := 1 to Length(S) do
    if S[i] = C then
      inc(result);
end;



字串(String)計數
1
2
3
4
5
6
7
8
9
10
11
12
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;

資料來源:

沒有留言:

張貼留言