For instance, source data:
some blabla, sentence, example
Awaited result:
[some,blabla,sentence,example]
I can split by comma, but don't know how to split by coma and space at the same time?
My source code, so far:
string.split("\\s*,\\s*")
To split a string by space or comma, pass the following regular expression to the split() method - /[, ]+/ . The method will split the string on each occurrence of a space or comma and return an array containing the substrings.
You can split a String by whitespaces or tabs in Java by using the split() method of java. lang. String class. This method accepts a regular expression and you can pass a regex matching with whitespace to split the String where words are separated by spaces.
split("\\s+") will split the string into string of array with separator as space or multiple spaces. \s+ is a regular expression for one or more spaces.
To split a string with dot, use the split() method in Java. str. split("[.]", 0); The following is the complete example.
You may use a set of chars as separator as described in Pattern
String string = "One step at,,a, time ,.";
System.out.println( Arrays.toString( string.split( "[\\s,]+" )));
Output:
[One, step, at, a, time, .]
\s : A whitespace character: [ \t\n\x0B\f\r]
[abc] : a, b, or c (simple class)
Greedy quantifiers X+ : X, one or more times
String.split("[ ,]+"); // split on on one or more spaces or commas
[]
- simple character class
[, ]
- simple character class containing space or comma
[ ,]+
- space or comma showing up one or more times
String source = "A B C,,D, E ,F";
System.out.println(Arrays.toString(source.split("[, ]+")));
Output:
[A, B, C, D, E, F]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With