Java - Split and trim in one shot

JavaStringSplit

Java Problem Overview


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", "faa baa", "fii bii"};

So my issue is how should to make split and trim in one shot i already split:

String[] result = attributes.split(",");

But the spaces still in the result :

String[] result = {" foo boo", " faa baa", " fii bii"};
                    ^           ^           ^

I know that we can make a loop and make trim for every one but I want to makes it in shot.

Java Solutions


Solution 1 - Java

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*");

Solution 2 - Java

Using java 8 you can do it like this in one line

String[] result = Arrays.stream(attributes.split(",")).map(String::trim).toArray(String[]::new);

Solution 3 - Java

If there is no text between the commas, the following expression will not create empty elements:

String result[] = attributes.trim().split("\\s*,+\\s*,*\\s*");

Solution 4 - Java

// 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

Solution 5 - Java

You can do it with Google Guava library this way :

List<String> results = Splitter.on(",").trimResults().splitToList(attributes);

which I find quite elegant as the code is very explicit in what it does when you read it.

Solution 6 - Java

What about spliting with comma and space:

String result[] = attributes.split(",\\s");

Solution 7 - Java

create your own custom function

private static String[] split_and_trim_in_one_shot(String string){
 String[] result  = string.split(",");
 int array_length = result.length;

 for(int i =0; i < array_length ; i++){
  result[i]=result[i].trim();
 }
 return result;

Overload with a consideration for custom delimiter

private static String[] split_and_trim_in_one_shot(String string, String delimiter){
 String[] result  = string.split(delimiter);
 int array_length = result.length;

 for(int i =0; i < array_length ; i++){
  result[i]=result[i].trim();
 }
 return result;




Solution 8 - Java

String result[] = attributes.trim().split("\\s*,[,\\s]*");

previously posted here: https://blog.oio.de/2012/08/23/split-comma-separated-strings-in-java/

Solution 9 - Java

Best way is:

value.split(",").map(function(x) {return x.trim()});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYCF_LView Question on Stackoverflow
Solution 1 - JavaRaman SahasiView Answer on Stackoverflow
Solution 2 - JavaAndrei OlarView Answer on Stackoverflow
Solution 3 - JavaMaxpleView Answer on Stackoverflow
Solution 4 - JavaNaveenView Answer on Stackoverflow
Solution 5 - JavaOlivier MasseauView Answer on Stackoverflow
Solution 6 - JavaΦXocę 웃 Пepeúpa ツView Answer on Stackoverflow
Solution 7 - JavaTimeTraxView Answer on Stackoverflow
Solution 8 - JavaDmitriy PichuginView Answer on Stackoverflow
Solution 9 - JavaMahantesh ChatterView Answer on Stackoverflow