unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.StrUtils,
  Vcl.ComCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    RichEdit1: TRichEdit;
    Button3: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    StatusBar1: TStatusBar;
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Edit1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  tm, xm, phone, tmphone: String;
  I: Integer;
  function IsNumberChar(aStr: AnsiString): Boolean;
  var
    I: Integer;
  begin
    Result := True;
    for I := 1 to Length(aStr) do
    if Not (Ansichar(aStr[I]) In ['0'..'9', 'a'..'z', 'A'..'Z']) then
    begin
      Result := False;
      Break;
    end;
  end;
  function ToUTF8Encode(str: string): string;
  var
    b: Byte;
  begin
    Result := '';
    for b in BytesOf(UTF8Encode(str)) do
      Result := Format('%s%s%.2x', [Result, '=', b]);
  end;
begin
  RichEdit1.Clear;
  for I := 0 to Memo1.Lines.Count - 1 do
  begin
    tm := Memo1.Lines[i];
    xm := copy(tm, 1, Length(tm) - 11);
    phone := RightStr(tm, 11);
    tmphone := '';
    tmphone := tmphone + MidStr(phone, 1, 1) + '-';
    tmphone := tmphone + MidStr(phone, 2, 3) + '-';
    tmphone := tmphone + MidStr(phone, 5, 3) + '-';
    tmphone := tmphone + MidStr(phone, 8, 4);
    if IsNumberChar(AnsiString(xm)) then
    begin
      RichEdit1.Lines.Add('BEGIN:VCARD');
      RichEdit1.Lines.Add('VERSION:3.0');
      RichEdit1.Lines.Add('N:;' + xm + ';;;');
      RichEdit1.Lines.Add('TEL;VOICE;WORK:' + tmphone);
      RichEdit1.Lines.Add('END:VCARD');
    end else
    begin
      xm := ToUTF8Encode(xm);
      RichEdit1.Lines.Add('BEGIN:VCARD');
      RichEdit1.Lines.Add('VERSION:3.0');
      RichEdit1.Lines.Add('N;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:;' + xm + ';;;');
      RichEdit1.Lines.Add('TEL;VOICE;WORK:' + tmphone);
      RichEdit1.Lines.Add('END:VCARD');
    end;
    tm := '';
    xm := '';
  end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  tm, xm, phone: String;
  I: integer;
begin
  for I := 0 to Memo1.Lines.Count - 1 do
  begin
    tm := Memo1.Lines[i];
    xm := copy(tm, 1, Length(tm) - 11);
    phone := RightStr(tm, 11);
    if Edit1.Text = phone then
    begin
      Label2.Caption := xm;
      exit;
    end;
  end;
end;

procedure TForm1.Edit1Click(Sender: TObject);
begin
  Edit1.SelectAll;
end;

end.
