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.
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.
Simple to Spit String by Space
OK, so we have to do splitting as you already got the answer I would generalize it.
If you want to split any string by spaces, delimiter(special chars).
First, remove the leading space as they create most of the issues.
First remove the leading space which can be space, tab etc.
Now if you want to split by space or any special char.
But as w contains [a-zA-Z_0-9] ,so if you want to split by underscore(_) also use
Since it's been a while since these answers were posted, here's another more current way to do what's asked:
Now you have a list of strings (which is arguably better than an array); if you do need an array, you can do
output.toArray(new String[0]);
Try this one
While the accepted answer is good, be aware that you will end up with a leading empty string if your input string starts with a white space. For example, with:
The result will be:
So you might want to trim your string before splitting it:
you can saperate string using the below code