I have a String like this : String attributes = \" foo boo, faa baa, fii bii,\"
I want to get a result like this :
String[] result = {\"foo boo\
Use regular expression \s*,\s*
for splitting.
String result[] = attributes.split("\\s*,\\s*");
For Initial and Trailing Whitespaces
The previous solution still leaves initial and trailing white-spaces. So if we're expecting any of them, then we can use the following solution to remove the same:
String result[] = attributes.trim().split("\\s*,\\s*");
// given input
String attributes = " foo boo, faa baa, fii bii,";
// desired output
String[] result = {"foo boo", "faa baa", "fii bii"};
This should work:
String[] s = attributes.trim().split("[,]");
As answered by @Raman Sahasi:
before you split your string, you can trim the trailing and leading spaces. I've used the delimiter
,
as it was your only delimiter in your string