Split the words using string tokenizer if it is followed by two or more space/whitespace in java? [duplicate]

StackOverflow https://stackoverflow.com/questions/15781234

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.]
Was it helpful?

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);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top