Und hier eine Klasse, die Telefonnummern in Abhängigkeit des Kontexts normalisiert und vergleicht
Delphi-Quellcode:
type
TPhoneContext = record
IntlPrefix : string;
IntlNumber : string;
NatPrefix : string;
NatNumber : string;
end;
const
MunichPhoneContext : TPhoneContext = ( IntlPrefix : '00'; IntlNumber : '49'; NatPrefix : '0'; NatNumber : '89' );
type
TPhoneNumberComparer = class
private
FContext : TPhoneContext;
function NumberClearing( const Number : string ) : string;
public
property Context : TPhoneContext read FContext write FContext;
function NormalizeNumber( const Number : string ) : string;
function IsSameNumber( const Number1, Number2 : string ) : Boolean;
end;
{ TPhoneNumberComparer }
function TPhoneNumberComparer.IsSameNumber( const Number1, Number2 : string ) : Boolean;
begin
Result := NormalizeNumber( Number1 ) = NormalizeNumber( Number2 );
end;
function TPhoneNumberComparer.NormalizeNumber( const Number : string ) : string;
begin
Result := NumberClearing( Number );
if Result.StartsWith( '+' ) then
Result := Context.IntlPrefix + Result.Substring( 1 );
if Result.StartsWith( Context.IntlPrefix + Context.IntlNumber ) then
begin
Result := Context.NatPrefix + Result.Substring( Length( Context.IntlPrefix + Context.IntlNumber ) );
end;
if Result.StartsWith( Context.NatPrefix + Context.NatNumber ) then
begin
Result := Result.Substring( Length( Context.NatPrefix + Context.NatNumber ) );
end;
end;
function TPhoneNumberComparer.NumberClearing( const Number : string ) : string;
var
LIdx : Integer;
LChar : Char;
begin
Result := '';
for LIdx := 0 to Number.Length - 1 do
begin
case Number.ToUpper.Chars[LIdx] of
'A', 'B', 'C' :
LChar := '2';
'D', 'E', 'F' :
LChar := '3';
'G', 'H', 'I' :
LChar := '4';
'J', 'K', 'L' :
LChar := '5';
'M', 'N', 'O' :
LChar := '6';
'P', 'Q', 'R', 'S' :
LChar := '7';
'T', 'U', 'V' :
LChar := '8';
'W', 'X', 'Y', 'Z' :
LChar := '9';
'+', '0' .. '9' :
LChar := Number.Chars[LIdx];
else
LChar := #0;
end;
if LChar <> #0 then
Result := Result + LChar;
end;
end;
UPDATE
Jetzt frisst das Teil eigentlich (fast) alles
Code:
NormalizeNumber:
+49 89 BEIL => 2345
+49 89 2345 => 2345
(089) 2345 => 2345
+48 89 234-5 => 0048892345
Diese (komischen) Konstrukte
frisst er nicht (mag er auch nicht)
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ea 0a 4c 14 0d b6 3a a4 c1 c5 b9
dc 90 9d f0 e9 de 13 da 60)