Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
www - 30 Jan 2007 21:27 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.