I would like to parse entire file based on all the possible delimiters like commas, colon, semi colons, periods, spaces, hiphens etcs.
Suppose I have a hypothetical string l开发者_JAVA技巧ine "Hi,X How-how are:any you?" I should get output array with items Hi,X,How,how,are,any and you.
How do I specify all these delimiter in String.split method?
Thanks in advance.
String.split
takes a regular expression, in this case, you want non-word characters (regex \W
) to be the split, so it's simply:
String input = "Hi,X How-how are:any you?";
String[] parts = input.split("[\\W]");
If you wanted to be more explicit, you could use the exact characters in the expression:
String[] parts = input.split("[,\\s\\-:\\?]");
精彩评论