Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to use regex to split a sentence with different spaces?

Reply
Thread Tools

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

 
 
www
Guest
Posts: n/a
 
      01-30-2007
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>
 
Reply With Quote
 
 
 
 
www
Guest
Posts: n/a
 
      01-30-2007
www wrote:
> 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.
 
Reply With Quote
 
 
 
 
Andreas Leitgeb
Guest
Posts: n/a
 
      01-30-2007
www <(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
how to apply different css styles to different part of sentence? Jeff ASP .Net 2 05-07-2009 12:44 PM
Split a sentence by regular expression Pranjal Jain Ruby 1 04-26-2008 09:06 AM
Perl Split on a Long Sentence Question Brian Wilkins Perl Misc 2 10-09-2006 05:01 PM
Split a sentence by punctuations using Python chad Python 2 03-05-2004 11:07 PM



Advertisments