I was doing a question out of the book oracle_certified_professional_java_se_7_programmer_exams_1z0-804_and_1z0-805 by Ganesh and Sharma.
One question is:
Consider the following program and predict the output:
class Test { public static void main(String args[]) { String test = "I am preparing for OCPJP"; String[] tokens = test.split("\\S"); System.out.println(tokens.length); } }
a) 0
b) 5
c) 12
d) 16
Now I understand that \S is a regex means treat non-space chars as the delimiters. But I was puzzled as to how the regex expression does its matching and what are the actual tokens produced by split.
I added code to print out the tokens as follows
for (String str: tokens){
System.out.println("<" + str + ">");
}
and I got the following output
16
<>
< >
<>
< >
<>
<>
<>
<>
<>
<>
<>
<>
< >
<>
<>
< >
So a lot of empty string tokens. I just do not understand this.
I would have thought along the lines that if delimiters are non space chars that in the above text then all alphabetic chars serve as delimiters so maybe there should be 21 tokens if we are matching tokens that result in empty strings too. I just don't understand how Java's regex engine is working this out. Are there any regex gurus out there who can shed light on this code for me?