Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I separate the server name from the port number in a string?

If there is a mail server string like smtp.gmail.com:587, how could I parse it to save "smtp.gmail.com" in a string variable and "587" in another?

like image 525
spspli Avatar asked Dec 03 '22 08:12

spspli


1 Answers

function SplitAtChar(const Str: string; const Chr: char;
  out Part1, Part2: string): boolean;
var
  ChrPos: integer;
begin
  result := true;
  ChrPos := Pos(Chr, Str);
  if ChrPos = 0 then
    Exit(false);
  Part1 := Copy(Str, 1, ChrPos - 1);
  Part2 := Copy(Str, ChrPos + 1, MaxInt);
end;

Sample usage:

var
  p1, p2: string;
begin
  if SplitAtChar('smtp.gmail.com:587', ':', p1, p2) then
  begin
    ShowMessage(p1);
    ShowMessage(p2);
  end;
like image 101
Andreas Rejbrand Avatar answered Jan 10 '23 03:01

Andreas Rejbrand