SOUNDEX(char_expr) 比较两个字符串的相似性后返回一个四位数代码。
DIFFERENCE(char_expr1,char_expr2) 比较两个字符串,返回值从0到4,4为最优匹配。
Delphi没有可用的函数,我写了一个:
function MaxMatchStr(DestStr:String;Strs:array of String):String;
var
I:Integer;
begin
Result:='';
for I:=1 to Length(Strs) do
//如果与目标匹配
if (Pos(Strs[I],DestStr)>0) and
//而且比现在找到的结果更长
(Length(Strs[I])>Length(Result)) then
//替换当前结果
Result:=Strs[I];
end;
演示:
procedure TForm1.Button1Click(Sender: TObject);
begin
Caption:=MaxMatchStr('123456789',
['1',
'12',
'123',
'1234',
'12345',
'123456',
'1234567',
'12345678',
'123456789']);
end;
////////////////////////////////////////////////////////////////////////////
Function CompChar(Str1,Str2:String):integer;
var TempStr:String;
i,j:Integer;
Begin
result:=0;
if Length(str2)>Length(str1) then begin
tempstr:=str1;
str1:=str2;
str2:=tempstr;
end;
for i:=length(Str1) downto 1 do
for j:=1 to Length(Str1)-i+1 do
if (Pos(copy(Str1,j,i),Str2)>0)and(result<i) then result:=i;
end;