I need to split a text using the separator ". ". For example I want this string :
Washington is the U.S Capital. Barack is living there.
To be cut into two parts:
Washington is the U.S Capital.
Barack is living there.
Here is my code :
// Initialize the tokenizer
StringTokenizer tokenizer = new StringTokenizer("Washington is the U.S Capital. Barack is living there.", ". ");
while (tokenizer.hasMoreTokens()) {
System.out.println(tokenizer.nextToken());
}
And the output is unfortunately :
Washington
is
the
U
S
Capital
Barack
is
living
there
Can someone explain what’s going on?
Don’t use
StringTokenizer; it’s a legacy class. Usejava.util.Scanneror simplyString.splitinstead.This prints:
Note that
splitandScannerare “regex”-based (regular expressions), and since.is a special regex “meta-character”, it needs to be escaped with\. In turn, since\is itself an escape character for Java string literals, you need to write"\\. "as the delimiter.This may sound complicated, but it really isn’t.
splitandScannerare much superior toStringTokenizer, and regex isn’t that hard to pick up.Regular expressions tutorials
Related questions
API Links
java.util.StringTokenizerStringTokenizeris a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use thesplitmethod ofStringor thejava.util.regexpackage instead.java.util.ScannerString[] String.splitBut what went wrong?
The problem is that
StringTokenizertakes each character in the delimiter string as individual delimiters, i.e. NOT the entireStringitself.From the API: