Reputation: 65
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.
Upvotes: 1
Views: 2133
Reputation: 34909
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;
Upvotes: 3
Reputation: 613053
If it were me I'd just start from the end of the string, and work back until I found the first space character. Your required text is what can be found to the right.
function FilterIP(const s: string): string;
var
i: Integer;
begin
i := Length(s);
while (i>=1) and (s[i]>' ') do
dec(i);
Result := Copy(s, i+1, MaxInt);
end;
Upvotes: 2
Reputation: 157
You can do it like this (if the IP is always at the end):
var tmp_str: String;
...
tmp_str:=Memo1.Lines[0]; //change the 0 to your desired line
while(Pos(' ', tmp_str)>0)do Delete(tmp_str, 1, Pos(' ', tmp_str));
Edit1.Text:=tmp_str;
Upvotes: 0