String.split() not giving proper results

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • giffy
    New Member
    • Aug 2008
    • 9

    String.split() not giving proper results

    HOW to get the spaces out of a delimited string ??
    here is a usage scenario where split() seems not to work
    Code:
    String origAVNListStr = "~`L~`L~`L~`~`~`L~`";
    String[] strArr = origAVNListStr.split("~`");
            
    for(int i=0;i<strArr.length;i++)
    {
           System.out.println(i+1 +"the next token - " + strArr[i]);
    }
    and the output is
    Code:
    1the next token -
    2the next token - L
    3the next token - L
    4the next token - L
    5the next token -
    6the next token -
    7the next token - L
    So as we can see - that the last space that shall be displayed as
    [HTML]8the next token -[/HTML] is not displayed.

    As far as this space reading from the string in an Array is concerned StringTokenizer fails even miserably. It will not take any of the spaces(either at the start/end or in between)

    Is there any way out of it. Sincere thanks in advance.
  • blazedaces
    Contributor
    • May 2007
    • 284

    #2
    Trailing spaces are automatically ignored when you use the split method... this is exactly what it's supposed to do.

    My question is, why do you need it to write the remaining space? What information does a remaining space give you?

    If you know that the number of strings in the array produced by split should be a minimum of, let's say in this case, 8, then why not make that for loop go 8 times and have an if/else statement like so to check if i is < the length of the array. Then just output blanks where you know they would be...

    -blazed

    Comment

    • giffy
      New Member
      • Aug 2008
      • 9

      #3
      Thanks for the prompt reply.

      It is a dynamic array situation. The code was just to test what is going wrong.

      In addition -
      1. I dun know the array size before hand
      2. Also the data can be many blanks at the end
      3. these are data fields that I need to handle as that only
      to be specific
      Code:
      String theData = "";

      Comment

      • blazedaces
        Contributor
        • May 2007
        • 284

        #4
        Originally posted by giffy
        Thanks for the prompt reply.

        It is a dynamic array situation. The code was just to test what is going wrong.

        In addition -
        1. I dun know the array size before hand
        2. Also the data can be many blanks at the end
        3. these are data fields that I need to handle as that only
        to be specific
        Code:
        String theData = "";
        Question, are you going through many lines of code, each of which may or may not have blanks at the end? Is it that you want them all to be the same size?

        I'm still confused as to why seeing the blanks or not is helping you in any way. It can only show you a lack of information.

        If you, for example, want all the arrays you obtain from a set of many strings, to be the same size, you could find the length of the longest array in the list and just go through the arrays, recreating a new one with that length and replacing the values along the way AFTER you read through the data once...

        Still, what exactly are you trying to do? If I knew that I could help more.

        Good luck,

        -blazed

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by giffy
          So as we can see - that the last space that shall be displayed as
          [HTML]8the next token -[/HTML] is not displayed.
          Note that any string can theoretically be interpreted as ending with infinitely
          many empty strings. The split method wisely ignores them all. If you insist
          maybe you can use the other (overloaded) two argument split() method. Check
          the API documentation for the String class for details.

          kind regards,

          Jos

          Comment

          • giffy
            New Member
            • Aug 2008
            • 9

            #6
            Thanks Guys. split() method with two arguments did the trick

            here is the code
            Code:
            String origAVNListStr = "~`L~`L~`L~`~`~`L~`";
            String[] strArr = origAVNListStr.split("~`", -1);
            for(int i=0;i<strArr.length;i++)
            {
                 System.out.println(i+1 +"the next token - " + strArr[i]);
            }
            and the output is as desired with all the sapces being returned and not ignored (at the end)
            Code:
            1the next token -
            2the next token - L
            3the next token - L
            4the next token - L
            5the next token -
            6the next token -
            7the next token - L
            8the next token -

            Comment

            • itsraghz
              New Member
              • Mar 2007
              • 124

              #7
              Well, that's a cool solution suggested by JosAh.

              I am also wondering as that blazed, what exactly you would be doing with just the count rather than the valid tokens?

              Comment

              Working...