Java 将字符串拆分为标记
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18739190/
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
Splitting a String into tokens
提问by user2682570
I would like to split a String into tokens that are stored in an array. However i don not think I am able to use delimiters as the strings information is not separated by a specific set of characters. The information is however always separated by varying amounts of white space.
我想将字符串拆分为存储在数组中的标记。但是我不认为我能够使用分隔符,因为字符串信息没有被一组特定的字符分隔。然而,信息总是由不同数量的空白分隔。
Like this:
像这样:
0 147 530.936 1 656.336 -1.12709 656.336 -0.52921 -0.0131993 -0.882138 0 20 0 0 0.878423 0 1.4013E-045 0
My question is, is there a way to use the varying amounts of white space as delimiters, in order to tokenize the string?
我的问题是,有没有办法使用不同数量的空格作为分隔符,以便对字符串进行标记?
采纳答案by Pshemo
How about
怎么样
String[] tokens = yourString.split("\s+");
Split uses regex and in regex
拆分使用正则表达式和正则表达式
- to represent any whitespace you can use
"\\s" - to say "one or more times" use
+
- 表示您可以使用的任何空格
"\\s" - 说“一次或多次”使用
+
回答by newuser
input = input.replaceAll("\s+", " ");
String[] splitArray = input.split(" ");

