You can use this regex
in the String.split(regex)
method.
String[] tokens = str.split(" +");
Update:-
If you need to remove the leading and trailing white spaces, trim()
it and then split your String.
str = str.trim();
Question
In my string, i want to tokenize string on the basis of two or more spaces.
E.x.
String str = "I am Chaitanya Gadam. Split this string."
StringTokenizer tokenizer = new StringTokenizer(str);
while (tokenizer.hasMoreTokens())
{
String token = tokenizer.nextToken();
System.out.println("==Token== : "+token);
}
I am getting out put as :
==Token== : [I]
==Token== : [am]
==Token== : [Chaitanya]
==Token== : [Gadam.]
==Token== : [split]
==Token== : [this]
==Token== : [string.]
But desired Output should be :
==Token== : [I am]
==Token== : [Chaitanya Gadam.]
==Token== : [split]
==Token== : [this string.]
Solution
You can use this regex
in the String.split(regex)
method.
String[] tokens = str.split(" +");
Update:-
If you need to remove the leading and trailing white spaces, trim()
it and then split your String.
str = str.trim();
OTHER TIPS
This should work-
String[] array = str.split("\\s{2,}");
for (String word : array) {
System.out.println(word);
}