Search code examples
delphimemo

Delphi converting memo output to edit.text


How can I convert a specific line of memo out put to a text edit box?

I would like to get specific IP address assigned to the TAP Adapter to text box and I add route of the IP in the text box but am stack on importing the IP to text box is there a better idea or a way I can query the IP from the TAP device adapter or any other simpler method?

net30,ping 5,ping-restart 10,socket-flags TCP_NODELAY,ifconfig 10.8.0.6 10.8.0.5'

Am aiming at the last IP the 10.8.0.5 to be imported to a text edit box.


Solution

  • Split the string with space delimiter using TStringHelper.Split and take the last string:

    function FilterIP(const s: String): String;
    var
      splitted: TArray<String>;
    begin
      if (s = '') then
        Result := ''
      else begin
        splitted := s.Split([' ']);
        Result := splitted[Length(splitted)-1];
      end;
    end;
    
    myEdit.Text := FilterIP(MyMemo[myLine]);
    

    You could also use StrUtils.SplitString to split the string.

    In Delphi-7 you could use DelimitedText in TStringList:

    sList.Delimiter := ' ';
    sList.DelimitedText := s;
    

    See here for other alternatives to split a string.


    As David mentioned in a comment, you could skip allocating unused strings by searching the space delimiter from the back of the string. This could be done with SysUtils.LastDelimiter:

    function FilterIP(const s: String): String;
    var
      lastIx: Integer;
    begin
      lastIx := LastDelimiter(' ',s);
      if (lastIx > 0) then
        Result := Copy(s,lastIx+1)
      else
        Result := '';
    end;