使用多个分隔符Java分割字符串 [英] Splitting string using multiple delimiters Java
问题描述
我想使用多个定界符分割一个字符串。现在我正在使用:
String delims = [\\s;。,:'!?()];
这对于这些字符似乎很好,但是当我尝试添加-
字符时,它对我大吼大叫。如何使用所有这些字符加上-
作为分隔符来分割我的字符串?提前致谢!
I would like to split a string using multiple delimiters. Right now I am using this:
String delims = "[\\s;.,:'!?()]";
which seems to work fine for those characters, but when I try to add the -
character, it yells at me. How can I use all of these characters plus -
as delimiters to split my string? Thanks in advance!
推荐答案
-
在字符类中具有特殊含义,表示范围。 (例如 [0-9]
将匹配任何数字。)
-
has a special meaning in character classes, indicating ranges. (E.g. [0-9]
will match any digit.)
但是,如果您输入的话作为第一个字符或最后一个字符,它将与文字-
匹配。
However, if you put if you put it either as the first character or the last it will be matched as a literal -
.
这篇关于使用多个分隔符Java分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!