Results 1 to 3 of 3
  1. #1
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    167
    Rep Power
    0

    Default Peculiar Error with string.split("0")

    Here is some short code that should sample the problem

    Java Code:
    class Sample {
    public static void main ( String[] args ) {
    String s = "001000";
    String[] tokens = s.split("0");
    for(int i = 0; i < tokens.length; i++) {
    System.out.println("Token #" + (i + 1) + ": " + tokens[i]);
    }
    }
    }
    When that is run, for some reason, tokens seems to be {"","","1"}, simply ignoring the remaining 0s!

    Why in the world is that happening?

    I need the output to be {"","","1","","",""}....


    EDIT:

    I'll add some info here. I looked up the string.split(regex) api and found the issue - all trailing empty tokens are discarded.
    Luckily, I found another version of string.split which takes two parameters, string.split(regex, limit), where if the limit is negative, trailing tokens that are empty are kept. The issue is that this adds an extra empty trailing token for some reason. Not sure what to do.
    Last edited by AlexGraal; 02-15-2014 at 05:08 AM.

  2. #2
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Peculiar Error with string.split("0")

    Quote Originally Posted by AlexGraal View Post
    The issue is that this adds an extra empty trailing token for some reason. Not sure what to do.
    Java Code:
    for(int i = 0; i < tokens.length - 1; i++) {
    System.out.println("Token #" + (i + 1) + ": " + tokens[i]);
    }
    1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,414
    Rep Power
    5

    Default Re: Peculiar Error with string.split("0")

    Quote Originally Posted by AlexGraal View Post
    I need the output to be {"","","1","","",""}....

    The issue is that this adds an extra empty trailing token for some reason. Not sure what to do.
    Not certain what you are talking about. The tokens are exactly what you said you wanted. Two empty strings, a one, and three empty strings.

    Java Code:
    String s = "001000";
    String[] tokens = s.split("0",-1);
    System.out.println(Arrays.toString(tokens)); // prints   [, , 1, , , ]
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 1
    Last Post: 11-02-2012, 11:38 PM
  2. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  3. Split string with the first "!" sign?
    By Lionlev in forum New To Java
    Replies: 2
    Last Post: 10-25-2012, 12:35 AM
  4. Replies: 10
    Last Post: 12-07-2010, 12:29 PM
  5. jsp insert into database error(java.lang.NumberFormatException: For input string: "")
    By cypher_girl in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 12-22-2009, 03:14 AM

Posting Permissions

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