我制作了一个 android 应用程序,它向控制台返回一些 Linux 命令。
在这种情况下,我想找到我的 wlan0 ip 和 wlan1(连接时)ip。
我得到了 ifconfig
命令返回的全文。问题是如何进入这 2 个 IP 的 2 个变量?
我尝试在 inet
字之后添加子字符串,但 inet
有超过 1 个字。最佳解决方案是什么?
这是我的字符串:
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 16436
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 37 bytes 7123 (6.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 37 bytes 7123 (6.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
p2p0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet6 xxxxxx prefixlen 64 scopeid 0x20<link>
ether xxxxxx txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.100 netmask 255.255.255.0 broadcast 192.168.0.255
inet6 xxxxxxxxxxx prefixlen 64 scopeid 0x20<link>
ether xxxxxxx6 txqueuelen 1000 (Ethernet)
RX packets 1504 bytes 817297 (798.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 686 bytes 126080 (123.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
请注意,我不关心 :p2p0
或 lo
部分,只关心 wlan0 和 wlan1(连接时)。
我要的是把192.168.0.100存成String ip;
提前致谢!
最佳答案
我不确定我是否理解你的尝试,但我认为 Regular Expressions 是一种可行的方法
关于java - 从多次出现的较大字符串中获取特定的小字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44077217/