Spork Spork - 7 months ago 22
Java Question

Using multiple delimiters for .split in Java

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.

Answer

Learn more about regex since you are using it.

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

or just:

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