Hi,
I saw the following code to split a sentence into words. My question is:
How could I modify the code to split a sentence like(in his example,
the words are separated by EXACTLY ONE SPACE by chance):
"But I'm not dead yet! I feel happy!"
<Code>
// : c12:ReplacingStringTokenizer.java
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
import java.util.Arrays;
import java.util.StringTokenizer;
public class ReplacingStringTokenizer {
public static void main(String[] args) {
String input = "But I'm not dead yet! I feel happy!";
StringTokenizer stoke = new StringTokenizer(input);
while (stoke.hasMoreElements())
System.out.println(stoke.nextToken());
System.out.println(Arrays.asList(input.split(" ")));
}
} ///:~
</Code>
www - 30 Jan 2007 21:43 GMT
> Hi,
>
[quoted text clipped - 21 lines]
> } ///:~
> </Code>
Sorry. I think I have got it:
System.out.println(Arrays.asList(input.split(" +")));
Add "+" after the space " " will do it.
Andreas Leitgeb - 30 Jan 2007 21:49 GMT
> I saw the following code to split a sentence into words. My question is:
> How could I modify the code to split a sentence like:
> "But I'm not dead yet! I feel happy!"
> [...]
> // From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
This seems to be a bit aged.
If I'm not mistaken, the java.util.StringTokenizer
is almost deprecated by now (as well as Enumeration):
" StringTokenizer is 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 the split method of String
" or the java.util.regex package instead.
Another class, that might serve your need is the java.util.Scanner
(which I was surprised not to have found referenced in StringTokenizer's
docu) It treats multiple immediately subsequent occurrances of the
delimiter as if there was only one.