function ExtractDomain(const URL : String) : String;
var
I, J : Integer;
begin
I := Pos('://', URL);
if I <= 0 then
I := 1
else
Inc(I, 3);
J := Pos('/', URL, I);
if J <= 0 then begin
Result := Copy(URL, I, MAXINT);
Exit;
end;
Result := Copy(URL, I, J - I);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Index : Integer;
Dict : TDictionary<String, Integer>;
URL : String;
Domain : String;
Value : Integer;
begin
Dict := TDictionary<String, Integer>.Create(10000);
try
ListBox1.Items.BeginUpdate;
try
for Index := ListBox1.Items.Count - 1 downto 0 do begin
URL := ListBox1.Items[Index];
if URL = '' then begin
ListBox1.Items.Delete(Index);
continue;
end;
Domain := ExtractDomain(Trim(UpperCase(URL)));
if Dict.TryGetValue(Domain, Value) then begin
// Domain already found, delete from ListBox
ListBox1.Items.Delete(Index);
continue;
end;
// Domain not seen before, add to dictionary and don't remove from list
Dict.Add(Domain, 0);
end;
finally
ListBox1.Items.EndUpdate;
end;
finally
FreeAndNil(Dict);
end;
end;
This will check for domain by ignoring character casing and ignoring the protocol.
You may adept this to a TStrings easily.
I used a dictionary because you said you have 10K items. A dictionary should be faster than a simple list when there are a lot of items but I have not checked how many items are required so that dictionary is faster than list.