Java 如何使用任何空格字符作为分隔符拆分字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/225337/
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
How to split a string with any whitespace chars as delimiters
提问by mcjabberz
What regex pattern would need I to pass to java.lang.String.split()
to split a String into an Array of substrings using all whitespace characters (' '
, '\t'
, '\n'
, etc.) as delimiters?
什么正则表达式模式需要我传递给 java.lang.String.split()
拆分成字符串使用的所有空格字符(子字符串数组' '
,'\t'
,'\n'
,等)作为分隔符?
采纳答案by Henrik Paul
Something in the lines of
某事在
myString.split("\s+");
This groups all white spaces as a delimiter.
这将所有空格分组为分隔符。
So if I have the string:
所以如果我有字符串:
"Hello[space][tab]World"
This should yield the strings "Hello"
and "World"
and omit the empty space between the [space]
and the [tab]
.
这应该产生字符串"Hello"
并"World"
省略[space]
和之间的空格[tab]
。
As VonC pointed out, the backslash should be escaped, because Java would first try to escape the string to a special character, and send thatto be parsed. What you want, is the literal "\s"
, which means, you need to pass "\\s"
. It can get a bit confusing.
正如 VonC 所指出的,应该对反斜杠进行转义,因为 Java 会首先尝试将字符串转义为特殊字符,然后将其发送以进行解析。你想要的是文字"\s"
,这意味着,你需要通过"\\s"
. 它可能会有点混乱。
The \\s
is equivalent to [ \\t\\n\\x0B\\f\\r]
.
在\\s
相当于[ \\t\\n\\x0B\\f\\r]
。
回答by VonC
"\\s+" should do the trick
"\\s+" 应该可以解决问题
回答by glenatron
In most regex dialects there are a set of convenient character summaries you can use for this kind of thing - these are good ones to remember:
在大多数正则表达式方言中,您可以使用一组方便的字符摘要来处理此类事情 - 这些是值得记住的:
\w
- Matches any word character.
\w
- 匹配任何单词字符。
\W
- Matches any nonword character.
\W
- 匹配任何非单词字符。
\s
- Matches any white-space character.
\s
- 匹配任何空白字符。
\S
- Matches anything but white-space characters.
\S
- 匹配除空白字符以外的任何内容。
\d
- Matches any digit.
\d
- 匹配任何数字。
\D
- Matches anything except digits.
\D
- 匹配除数字以外的任何内容。
A search for "Regex Cheatsheets" should reward you with a whole lot of useful summaries.
搜索“Regex Cheatsheets”应该会为您提供大量有用的摘要。
回答by Rishabh
Since it is a regular expression, and i'm assuming u would also not want non-alphanumeric chars like commas, dots, etc that could be surrounded by blanks (e.g. "one , two" should give [one][two]), it should be:
因为它是一个正则表达式,我假设你也不希望非字母数字字符,如逗号、点等可能被空格包围(例如,“一,二”应该给出 [one][two]),它应该是:
myString.split(/[\s\W]+/)
回答by Mike Manard
To get this working in Javascript, I had to do the following:
为了让它在 Javascript 中工作,我必须执行以下操作:
myString.split(/\s+/g)
回答by Felix Scheffer
Apache Commons Lang has a method to split a string with whitespace characters as delimiters:
Apache Commons Lang 有一种方法可以用空格字符作为分隔符来分割字符串:
StringUtils.split("abc def")
This might be easier to use than a regex pattern.
这可能比正则表达式模式更容易使用。
回答by RajeshVijayakumar
you can split a string by line break by using the following statement :
您可以使用以下语句按换行符拆分字符串:
String textStr[] = yourString.split("\r?\n");
you can split a string by Whitespace by using the following statement :
您可以使用以下语句按空格拆分字符串:
String textStr[] = yourString.split("\s+");
回答by jake_astub
Also you may have a UniCode non-breaking space xA0...
你也可能有一个 UniCode 不间断空格 xA0 ...
String[] elements = s.split("[\s\xA0]+"); //include uniCode non-breaking
回答by Olivia Liao
String str = "Hello World";
String res[] = str.split("\s+");
回答by Arrow
String string = "Ram is going to school";
String[] arrayOfString = string.split("\s+");