我想在Delphi中创建一个计算不同级别的两个字符串的函数。如果两个字符串相等(忽略大小写),那么它应该返回0,但如果它们不相等,它应该返回不同字符的数量。此功能对检查拼写非常有用。如何计算两个字符串之间的差异?
function GetDiffStringLevel(S1,S2:string):Integer;
begin
if SameText(S1,S2) then Exit(0);
// i want get different chars count
end
样本代码:
Diff:=GetDiffStringLevel('Hello','Hello');// Diff:=0;
Diff:=GetDiffStringLevel('Hello','2Hello');// Diff:=1;
Diff:=GetDiffStringLevel('Hello','H2ello');// Diff:=1;
Diff:=GetDiffStringLevel('Hello','Hello W');// Diff:=2;
Diff:=GetDiffStringLevel('Hello','World');// Diff:=6; or 5
另请参阅:[需要例程来检测类似但不相同的字符串](http://stackoverflow.com/q/10402858/576719)。 –