0

Given the sentence "The brown fox jumped the fence jumped. The jumped fox? The fox. Jumped" splitting by whitespace shows an empty string in the array. I expected words or words with punctuation. Where did this empty string come from in the sentence?

1
  • 7
    Can you specify your question in terms of actual Java code? Commented May 5, 2018 at 18:02

2 Answers 2

6

It's because your string contains consecutive whitespace characters, e.g.

jumped.  The
       ^^

There is a zero-length string between those two whitespaces, so if you split on every whitespace character you get the empty string between them.

If you want to match those as a single delimiter, use a quantifier, e.g.

"\\s+"
Sign up to request clarification or add additional context in comments.

1 Comment

I edited the question to put the string in backticks (I didn't otherwise change it), which confirms that the string has double spaces. +1
-1

I just code this on Eclipse. It works as expected. Here's the code: -

public class TokenizeString {

    public static void main(String[] args) {
        String stringToTokenize = "The brown fox jumped the fence jumped. The jumped fox? The fox. Jumped";
        String[] tokenizedStrings = stringToTokenize.split("\\s");
        for(String tokenizedString : tokenizedStrings) {
            if ("".equalsIgnoreCase(tokenizedString)) {
                System.out.println(true);
            } else {
                System.out.println(false);
            }
        }
    }

}

And here's the output: -

false false false false false false false false false false false false false

4 Comments

change " " to ""
" " isn't the empty string.
Even after changing that, the responses are all false.
The OP has edited the post to show the true string which caused the problem. The actual string should have been provided to begin with. If the OP used the original string provided in the post then he/she would not of had a problem. In my opinion the expression should always be "\\s+" just in case this sort of thing arises (unless null string elements are expected).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.