5

Right now my code only separates words by white space, but I also want to separate by '.' and "," too. Here is my current code:

for (String words : input.split("\\s+"))

For example, if the user entered "bread,milk,eggs" or "Um...awkss" It would consider that one word, and I want each word to be it's own word.

And while I'm here, I can't get

input.isAlpha() 

to work either.

4

3 回答 3

8

您可以使用此正则表达式进行拆分

input.split("\\s+|.+|,+")

或者简单地说:

input.split("[\\s.,]+")

请记住,点不必在方括号内转义

于 2013-11-14T01:47:04.640 回答
5

使用括号

for (String words : input.split("[\\s.,]+"))

当您想要括号中的任何字符时使用括号,这+意味着字符可以组合一次或多次。创建一个单独的分隔符,即space and periodor comma and space

于 2013-11-14T01:46:07.700 回答
0

你可以用这个

mySring = "abc==abc++abc==bc++abc";
String[] splitString = myString.split("\\W+");

正则表达式 \W+ ---> 它将根据非单词字符拆分字符串。

于 2016-04-16T22:48:49.587 回答