Author: Tomas Rutkauskas
Does anyone know how or does anyone know of a good procedure to match two strings?
What I want is a % match between two strings. Something like Hart and Harts are 80%
equal.
Answer:
1 uses2 math;
3 4 function IsStrMatch(s1, s2: string): Double;
5 var6 i, iMin, iMax, iSameCount: Integer;
7 begin8 iMax := Max(Length(s1), Length(s2));
9 iMin := Min(Length(s1), Length(s2));
10 iSameCount := -1;
11 for i := 0 to iMax do12 begin13 if i > iMin then14 break;
15 if s1[i] = s2[i] then16 Inc(iSameCount)
17 else18 break;
19 end;
20 if iSameCount > 0 then21 Result := (iSameCount / iMax) * 100
22 else23 Result := 0.00;
24 end;