Java String.split("\\s+")
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13533677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java String.split("\\s+")
提问by Tim
I am trying to parse some output. The output is as follows:
我正在尝试解析一些输出。输出如下:
raidz1-0 ONLINE 0 0 0
raidz1-0 ONLINE 0 0 0
My code is as follows:
我的代码如下:
String line = "\traidz1-0 ONLINE 0 0 0";
String[] tokens = line.split("\s+");
tokens ends up being {"raidz1-0", "ONLINE", "0"}
代币最终成为 {"raidz1-0", "ONLINE", "0"}
For some reason the last 2 zeros are discarded. I would like to retain the zeros, please tell me how.
由于某种原因,最后 2 个零被丢弃。我想保留零,请告诉我如何。
回答by jlordo
This code:
这段代码:
String line = "\traidz1-0 ONLINE 0 0 0";
String[] tokens = line.split("\s+");
System.out.println(tokens.length);
System.out.println(Arrays.toString(tokens));
prints:
印刷:
6
[, raidz1-0, ONLINE, 0, 0, 0]
回答by Rohit Jain
Well, it works fine in my case, and it should work: -
嗯,在我的情况下它工作正常,它应该工作: -
String line = "\traidz1-0 ONLINE 0 0 0";
String[] tokens = line.split("\s+");
System.out.println(Arrays.toString(tokens));
OUTPUT : -
输出 : -
[, raidz1-0, ONLINE, 0, 0, 0]
Are you sure, you didn't get the last two zeros?
你确定,你没有得到最后两个零?