使用字符串的Java方法

时间:2013-02-26 17:23:28

标签: java string methods

请我帮忙解决这个问题:

String d = ret.getData(); //I get the string using this
int ui;
ui = d.indexOf("=", d.indexOf("=")+1);
do{
int f=d.indexOf("=", ui+1 );
System.out.println(d.substring(ui+1, f-1));//Here I'm showing the results of my code
ui = ui + d.indexOf("=", ui);}
while ((d.indexOf("=") + ui)!= 0);

我拥有的字符串就是这个:

!re
=.id=*1
=name=ether1
=mtu=1500
=l2mtu=1600
=mac-address=D4:CA:6D:37:44:26
=arp=enabled
=auto-negotiation=true
=full-duplex=true
=speed=100Mbps
=running=true
=slave=false
=disabled=false

我的代码的结果是:

*1
ether1
l2mt
24:22
nin

我想得到的是:

*1
ether1
1500
1600
D4:CA:6D:37:44:26
enabled
true
true
100Mbps
true
false
false

我需要获得“=”

之后的所有内容

请帮忙!感谢

5 个答案:

答案 0 :(得分:0)

也许您可以使用Scanner,并使用“=”作为分隔符。只需运行scanner.next()并读入每个令牌:)

答案 1 :(得分:0)

您可以使用d.split("=")方法获取结果数组的最新值

答案 2 :(得分:0)

就这样做。

Scanner scan = new Scanner( ret.getData() );
scan.nextLine(); //skip the first line

While (scan.hasNextLine()){
     String s = scan.nextLine();
     int index = s.lastIndexOf("=");
     System.out.println(s.substring(index+1));
}

答案 3 :(得分:0)

尝试在=的string.split函数的帮助下拆分字符串。它将返回字符串数组。现在进行循环递增两次并显示输出。

答案 4 :(得分:0)

分离器会有所帮助,并且是最简单,最正确的方法。但是,如果您想以其他方式触摸鼻子或寻找替代方案,那么您可以使用正则表达式/模式匹配器。 : - )

public static void main(String[] args) {
    TestClass test = new TestClass();


    String d = "=mtu=1500"; //I get the string using this
    Scanner scan = new Scanner(d);
    System.out.println(scan.nextLine().split("=")[2]);


}

这将打印 - 1500