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

Discussion in 'Java' started by www, Jan 30, 2007.

  1. www

    www Guest

    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, Jan 30, 2007
    #1
    1. Advertising

  2. www

    www Guest

    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.
     
    www, Jan 30, 2007
    #2
    1. Advertising

  3. www <> 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.
     
    Andreas Leitgeb, Jan 30, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chad
    Replies:
    2
    Views:
    1,393
    Peter Otten
    Mar 5, 2004
  2. Replies:
    16
    Views:
    519
  3. Jeff
    Replies:
    2
    Views:
    5,130
  4. Pranjal Jain
    Replies:
    1
    Views:
    299
    Peter Szinek
    Apr 26, 2008
  5. Brian  Wilkins

    Perl Split on a Long Sentence Question

    Brian Wilkins, Oct 9, 2006, in forum: Perl Misc
    Replies:
    2
    Views:
    97
    -berlin.de
    Oct 9, 2006
Loading...

Share This Page