How to split a String by space

Total
0
Shares

I need to split my String by spaces.
For this I tried:

str = "Hello I'm your String";
String[] splited = str.split(" ");

But it doesn’t seem to work.


Solution

What you have should work. If, however, the spaces provided are defaulting to… something else? You can use the whitespace regex:

str = "Hello I'm your String";
String[] splited = str.split("\s+");

This will cause any number of consecutive spaces to split your string into tokens.

Source: StackOverflow.com

Leave a Reply

Your email address will not be published. Required fields are marked *