Results 1 to 4 of 4
  1. #1
    vipinvijay is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Default problem in spliting a text

    Hi,
    I am using "\\s+" to seperate string on behalf of multiple space. But when the space occur at the starting of string in that case it took that as a token. Plz tell me the reason behind it. While the same thing works in StringTokenizer

    String input=" one two three four five ";
    //using split method
    String[] outputArr=input.split("\\s+");
    for(int i=0;i<outputArr.length;i++)
    System.out.println("Element["+i+"]:"+outputArr[i]);
    //using tokenizer
    StringTokenizer str=new StringTokenizer(input);
    while(str.hasMoreElements())
    System.out.println("Element:"+str.nextToken());

  2. #2
    charleyjoyce is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    you will have to read the StringTokenizer API to see how it works (see especially the part where it explains what a token represents). one way to solve your problem if you don't want the extra space in front is to do a trim() first before splitting.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by vipinvijay View Post
    Hi,
    I am using "\\s+" to seperate string on behalf of multiple space. But when the space occur at the starting of string in that case it took that as a token. Plz tell me the reason behind it. While the same thing works in StringTokenizer

    String input=" one two three four five ";
    //using split method
    String[] outputArr=input.split("\\s+");
    for(int i=0;i<outputArr.length;i++)
    System.out.println("Element["+i+"]:"+outputArr[i]);
    //using tokenizer
    StringTokenizer str=new StringTokenizer(input);
    while(str.hasMoreElements())
    System.out.println("Element:"+str.nextToken());
    The String.split( ... ) method treats the delimeters as tokens that terminate other tokens, e.g. the String " foo bar ", when split with "\\s+" is treated as three separate tokens: "", "foo" and "bar". It's a bit of a nuisance ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    You can always split on just the character " ". This works fine fine if there are only single spaces - your version is more flexible, but requires more toying with.

Similar Threads

  1. Spliting a string (45 Characters)
    By BobAmin in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 11-20-2010, 09:45 AM
  2. spliting a string and checking each token's format
    By Implode in forum New To Java
    Replies: 1
    Last Post: 10-18-2009, 08:41 PM
  3. Spliting the Panel to three panels
    By suraw in forum New To Java
    Replies: 0
    Last Post: 03-25-2009, 06:05 PM
  4. Problem With Text Fields!
    By freshoreo in forum AWT / Swing
    Replies: 3
    Last Post: 08-04-2008, 09:52 PM
  5. Spliting desktop
    By nagarte in forum AWT / Swing
    Replies: 5
    Last Post: 05-05-2008, 12:56 PM

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
  •