Varuna Seneviratna wrote:. . . I think by a 0-length String you mean a String without even a space. . . .
Yes, I do.
It is not practically possible to have a 0-length String within a String . . .
Of course it is; the 0‑length String is implicitly and trivially a substring of every String object.
Try splitting the text you were shown with
"\\d", or
"[0-9]" on JShell, or print the resultant array with added quotes (\u201c/d):-
...and see how many empty Strings you get. The options
2$ and
1$ allow me to print
index first and increment it later. The tutorial link below explains somewhere what
"\\d" means.
What is meant by "any positive number of characters"? . . .
As I used it, it only means anything in the context of a regular expression. It means 1, 2, 3, 4, 5... ∞ repetitions of the
pattern shown before. As I showed it, it allows any amount of whitespace with a positive length. No, it doesn't mean to split on a 0‑length String.
. . . provided that the "\\s" splits on any positive number of characters . . .
No, it doesn't. It splits on one character. If you want to split on any positive number of characters, you need
"\\s+". There is a good introduction to regular expressions in the
Java™ Tutorials.
(Why aren't the delimiters displayed in the output of the above) . . .
Why are you using StringTokenizer? It has been marked as legacy code for eighteen years. You have activated the option to add the delimiters back to the split Strings, which option
String#split() doesn't have.
StringTokenizer seems to use a different method for splitting, not using regular expressions.