【问题标题】:Java method using Strings使用字符串的 Java 方法
【发布时间】:2013-02-12 06:31:58
【问题描述】:

请在此方法方面需要帮助:

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

我需要获取 "="

之后的所有内容

请帮忙!谢谢

【问题讨论】:

标签: java string methods


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    只要这样做。

    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】:

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 是的,并打印出数组中的事件值,因为您的数据是统一的。
        【解决方案5】:

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

        【讨论】:

          猜你喜欢
          • 2013-04-13
          • 1970-01-01
          • 1970-01-01
          • 2013-04-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多