How to use regex to split a sentence with different spaces?

W

www

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>
 
W

www

www said:
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>

Sorry. I think I have got it:

System.out.println(Arrays.asList(input.split(" +")));

Add "+" after the space " " will do it.
 
A

Andreas Leitgeb

www said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top