1

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.

1
  • Take all the text after the final space. Have you done any programming with strings before? Do you know how to extract parts of strings? Commented Jun 20, 2016 at 7:13

3 Answers 3

3

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;
Sign up to request clarification or add additional context in comments.

4 Comments

Am using Delphi 7 so am getting an error on this line splitted: TArray<String>;
Seems like a shame to do all that work splitting the entire string, creating an array and all those strings, only to throw them away.....
@DavidHeffernan, added an example without splitting into strings using the RTL. Not sure if LastDelimiter is in D7 though.
LastDelimiter is present in D7
2

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;

Comments

0

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;

15 Comments

Really pointless to perform all that heap allocation. Why not start at the right hand end of the string and read back until you find space. Further, duplicated calls to Pos are inefficient and ugly.
It's one line of string with about 10 calls of Pos. We're not in 1970s to save every bit of memory and cycle of CPU.
Performance matters just as much now as it did then, and the duplication is arguably a greater sin
I agree that performance is important but if it's only one line than it won't do any change. Of course you can check for the pos only once in a cycle and save it to the variable. But there is no big difference between my and your solution.
@jano152 thanks alot your method is working but its capturing a closing this quote sign too 10.8.0.5' how can i remove it off
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.