Results 1 to 5 of 5
  1. #1
    redasu is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default String Tokenizer

    I am using the string tokenizer in java:
    StringTokenizer st = new StringTokenizer(line," "); and I want to use a white space symbol as a seperator. Do u know a symbol as u can see I am using a simple white space but it doesn't include tabs and other white spaces.

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    StringTokenizer has been replaced with String.split():

    Java Code:
    String[] splt = line.split("[ \n\r\t]");
    \n - newline
    \r - return
    \t - tab

    did i miss any?
    My Hobby Project: LegacyClone

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Since it accepts a regex String, how about a greedy regex using the white space character class:
    Java Code:
    String[] tokens = line.split(\\s+);
    This way you don't have multiple splits if there is more than one white space character next to each other.

    e.g.,
    Java Code:
    import java.util.Arrays;
    
    public class Fu3 {
      public static void main(String[] args) {
        String test = "Hello Java! \n Goodbye world! \t\t What the heck? \t\n\tyoiks!";
        System.out.println(test);
        
        String regex1 = "[ \n\r\t]";
        String regex2 = "\\s+";
        
        String[] tokens1 = test.split(regex1);
        String[] tokens2 = test.split(regex2);
        
        System.out.println(Arrays.toString(tokens1));
        System.out.println(Arrays.toString(tokens2));
      }
    }

  4. #4
    redasu is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default "\\s+" doesn't work

    Firstly I don't know if there is a difference between the split method and the tokenizer one but the "\\s+" doesn't work it takes the letter s as a separator

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by redasu View Post
    Firstly I don't know if there is a difference between the split method and the tokenizer one
    Yes, there's a huge difference between the two. For one, split uses regular expressions and tokenizer doesn't.

    but the "\\s+" doesn't work it takes the letter s as a separator
    Of course it doesn't work. No one suggested that you use that with tokenizer.

Similar Threads

  1. Manipulating String Tokenizer
    By Bomber_Will in forum New To Java
    Replies: 2
    Last Post: 04-19-2009, 11:54 PM
  2. string tokenizer
    By twinytwo in forum New To Java
    Replies: 2
    Last Post: 03-26-2009, 02:10 PM
  3. Problem with string tokenizer
    By twinytwo in forum AWT / Swing
    Replies: 2
    Last Post: 03-26-2009, 11:27 AM
  4. String Tokenizer And Arraya
    By everlast88az in forum Advanced Java
    Replies: 2
    Last Post: 11-06-2008, 11:20 PM
  5. question on string tokenizer
    By munigantipraveen in forum New To Java
    Replies: 2
    Last Post: 05-23-2008, 05:00 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •