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?
-
7Can you specify your question in terms of actual Java code?tadman– tadman2018-05-05 18:02:10 +00:00Commented May 5, 2018 at 18:02
Add a comment
|
2 Answers
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+"
1 Comment
yshavit
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
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
Iłya Bursov
change
" " to ""Andy Turner
" " isn't the empty string.Rakesh Narang
Even after changing that, the responses are all false.
DevilsHnd - 退した
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).