uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, System.RegularExpressions,
function CompareStrings(List: TStringList; Index1, Index2: Integer): Integer; begin Result := StrToInt(List.ValueFromIndex[Index2]) - StrToInt(List.ValueFromIndex[Index1]); end; procedure TForm1.Button1Click(Sender: TObject); var InputString: string; SubString: string; Matches: TMatchCollection; Match: TMatch; SubStringCounts: TStringList; begin Memo1.Clear; InputString := 'www,eee,rrr,rrr,rrr,eee,r'; SubStringCounts := TStringList.Create; // 使用正则表达式匹配字符串 System.RegularExpressions, Matches := TRegEx.Matches(InputString, '\w+'); // 统计各个子字符串出现的次数 for Match in Matches do begin SubString := Match.Value; if SubStringCounts.IndexOfName(SubString) = -1 then SubStringCounts.Add(SubString + '=1') else SubStringCounts.Values[SubString] := IntToStr(StrToInt(SubStringCounts.Values[SubString]) + 1); end; // 输出结果 for SubString in SubStringCounts do begin Memo1.Lines.Add(SubString) end; SubStringCounts.Free; end;
procedure TForm1.Button2Click(Sender: TObject); var InputString: string; SubString: string; Matches: TMatchCollection; Match: TMatch; SubStringCounts: TStringList; begin Memo1.Clear; InputString := 'www,eee,rrr,rrr,rrr,eee'; SubStringCounts := TStringList.Create; // 使用正则表达式匹配字符串 Matches := TRegEx.Matches(InputString, '\w+'); // 统计各个子字符串出现的次数 for Match in Matches do begin SubString := Match.Value; if SubStringCounts.IndexOfName(SubString) = -1 then SubStringCounts.Add(SubString + '=1') else SubStringCounts.Values[SubString] := IntToStr(StrToInt(SubStringCounts.Values[SubString]) + 1); end; // 对统计结果进行排序,出现次数最多的放在最前面 SubStringCounts.CustomSort(CompareStrings); // 输出排序后的结果 for SubString in SubStringCounts do begin Memo1.Lines.Add(SubString) end; SubStringCounts.Free; end;
标签:SubString,begin,end,Matches,个数,逗号,SubStringCounts,排序,rrr From: https://www.cnblogs.com/tulater/p/18206403