delphi的Memo内容统计出字符出现的次数代码
delphi的Memo内容统计出字符出现的次数
Memo1为可输入对象,如何对Mome1的内如以"逗号"统计出对象出现的次数?
例如:
01,03,05,01,06,08,06,12
统计出结果如下形式:
================================
01出现了2次;06出现了2次;
03出现了1次;
05出现了1次;
08出现了1次;
12出现了1次
var tmpList,sumList : TStringList; I,tmpInd : Integer; begin tmpList := TStringList.Create; sumList := TStringList.Create; tmpList.Clear; tmpList.CommaText := '01,03,05,01,06,08,06,12'; for I := 0 to tmpList.Count - 1 do begin tmpInd := sumList.IndexOfName(tmpList[I]); if tmpInd = -1 then begin sumList.Add(tmpList[I] + '=1'); end else begin sumList.Values[tmpList[I]] := sumList.Values[tmpList[I]] + 1; end; end;
var tmpList,sumList : TStringList;
I,tmpInd : Integer;
begin
tmpList := TStringList.Create;
sumList := TStringList.Create;
tmpList.Clear;
tmpList.CommaText := '01,03,05,01,06,08,06,12';
for I := 0 to tmpList.Count - 1 do begin
tmpInd := sumList.IndexOfName(tmpList[I]);
if tmpInd = -1 then begin
sumList.Add(tmpList[I] + '=1');
end else begin
sumList.Values[tmpList[I]] := inttostr(strtoint(sumList.Values[tmpList[I]]) + 1);
end;
memo2.Text := sumList.Text;
end;
end;