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.

scott_lotus
  • 3,171
  • 22
  • 51
  • 69
Jk Robbin
  • 65
  • 10
  • 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? – David Heffernan Jun 20 '16 at 07:13

3 Answers3

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;
Community
  • 1
  • 1
LU RD
  • 34,438
  • 5
  • 88
  • 296
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;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
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;
jano152
  • 147
  • 1
  • 7
  • 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. – David Heffernan Jun 20 '16 at 08:10
  • 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. – jano152 Jun 20 '16 at 08:50
  • Performance matters just as much now as it did then, and the duplication is arguably a greater sin – David Heffernan Jun 20 '16 at 09:07
  • 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 Jun 20 '16 at 09:17
  • @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 – Jk Robbin Jun 20 '16 at 11:02
  • @JkRobbin Have you thought about trying to solve that problem for yourself? It really is quite easy. I cannot believe you are incapable of solving it yourself. On the face of it, we might conclude that you have decided to be lazy and get us to do your job for you. Perhaps you could prove me wrong. – David Heffernan Jun 20 '16 at 11:15
  • Thanks alot guys managed to find an exact line that ended with the dynamic ip given var str,matri1,matri2,dhcp:string; begin str:= memo1.Lines[46]; while(pos(' ', str)>0)do delete(str, 1, Pos(' ', str)); edit12.Text:= str; matri1:= 'route add 0.0.0.0 mask 128.0.0.0 '+dhcp+' metric 20'; matri2:= 'route add 128.0.0.0 mask 128.0.0.0 '+dhcp+' metric 20'; ShellExecute(Handle, 'open', PChar('cmd.exe'), PChar('/k '+matri1), nil, SW_HIDE); ShellExecute(Handle, 'open', PChar('cmd.exe'), PChar('/k '+matri2), nil, SW_HIDE); – Jk Robbin Jun 20 '16 at 11:32
  • @JkRobbin I thought that the ' is just an error in the original question. You can just add this line after the cycle: `Delete(tmp_str, Length(tmp_str), 1);` – jano152 Jun 20 '16 at 11:42
  • Strange that you should decide to use what is easily the worst of the three answers. – MartynA Jun 20 '16 at 12:56
  • Strange that you should feel urge to talk about it. – jano152 Jun 20 '16 at 13:25
  • @jano152: Well, the OP obviously isn't too familiar with string manipulation in Delphi so ought to benefit from having e.g. the elegance of DavidH's solution compared with this one pointed out. – MartynA Jun 20 '16 at 18:24
  • @MartynA Maybe he tried them all and liked this one the most. And in the process learned something from every one of the answers. – jano152 Jun 20 '16 at 18:37
  • @jano152 Maybe, but strange then that he should get stuck on the trailing quote applying your answer ... – MartynA Jun 20 '16 at 18:43
  • @jano152 sorry to disturb you but what if i want to capture a last line that has spaces like this Local Area Connection How do i go about it thanks. – Jk Robbin Jun 24 '16 at 03:36
  • @JkRobbin You mean that you have a N number of lines and want to find the last one which contains spaces? Like this: `var cycle, line: Integer; ... line:=-1; for cycle:=Memo1.Lines.Count-1 downto 0 do begin if(Pos(' ', Memo1.Lines[cycle])>0)then begin line:=cycle; Break; end; end; //your line will be stored in the line variable, if it's -1 then there aren't any lines with a space` – jano152 Jun 24 '16 at 06:43