"SOMETHING, SOMETHING1, SOMETHING2,..."
How can I split that string to
SOMETHING SOMETHING1 SOMETHING2
This is what I have at the moment:
Pattern p = Pattern.compile("\\,+");
Matcher 开发者_开发问答m = p.matcher(nVI);
while(m.find()){
System.out.println(m.group(1));
However, it is not producing the desired result.
What exactly separates the parts? Just a comma and a single space?
Try this:
String[] parts = nVI.split(", ");
There is no need to escape the comma by writing \\,
in your regular expression.
String s = "SOMETHING, SOMETHING1, SOMETHING2";
String[] stringList = s.split(", ");
for(String str : stringList){
System.out.println(str);
}
Here's a more generic solution (splitting by non-word characters):
String[] stringList = s.split("\\W+");
From the Pattern javadocs:
\w A word character: [a-zA-Z_0-9]
\W A non-word character: [^\w]
Or if you use Guava, you can do the equivalent (actually not quite equivalent, as it also matches non-ascii letters):
for(String word:
Splitter.on(CharMatcher.JAVA_LETTER_OR_DIGIT.negate()).split(str)){
// do something
}
精彩评论