1 2 Previous Next 15 Replies Latest reply on Apr 10, 2007 3:54 PM by 807606

    Using String.split() with '.' as a delimiter

    807606
      I'm trying to parse a string with the . as the delimiter and am not having any luck.
      String value = new String ("radioOutput.1");
      
      String[] record = value.split ("\0056");
      should fill record[0] with "radioOutput" and record[1] with "1", but I'm only getting a single String back that contains the entire starting String ("radioOutput.1").

      I've also tried
      String[] record = value.split ("\056");
      and it returned an array with zero length.

      Can anyone tell me what I'm doing wrong?
        • 1. Re: Using String.split() with '.' as a delimiter
          796125
          Off the top of my head, I'd say that it has something to do with the escaped character you're using there.

          What happes if you call this: System.out.println("\0056"); (sorry, I'm too lazy to check this myself)

          - Adam
          • 2. Re: Using String.split() with '.' as a delimiter
            796125
            I'm trying to parse a string with the . as the
            delimiter and am not having any luck.
            String value = new String ("radioOutput.1");
            
            String[] record = value.split ("\0056");
            should fill record[0] with "radioOutput" and
            record[1] with "1", but I'm only getting a single
            String back that contains the entire starting String
            ("radioOutput.1").

            I've also tried
            String[] record = value.split ("\056");
            and it returned an array with zero length.

            Can anyone tell me what I'm doing wrong?
            Hmm... You are trying to split an String on a delimeter, and the only character in the String IS the delimeter. Therefore, there is nothing else in the String, so it returns an empty array.

            - Adam
            • 3. Re: Using String.split() with '.' as a delimiter
              807606
              Off the top of my head, I'd say that it has something
              to do with the escaped character you're using there.

              What happes if you call this:
              System.out.println("\0056"); (sorry, I'm too lazy
              to check this myself)

              - Adam
              I get a spade character and the number 6.

              If I change it to just System.out.println ("\056") I get a .
              So, I would think that would mean that I should use \056 as my delimiter, but when I do, String.split() returns an empty array.
              • 4. Re: Using String.split() with '.' as a delimiter
                800351
                I'm trying to parse a string with the . as the
                delimiter and am not having any luck.
                String value = new String ("radioOutput.1");
                
                String[] record = value.split ("\0056");
                should fill record[0] with "radioOutput" and
                record[1] with "1", but I'm only getting a single
                String back that contains the entire starting String
                ("radioOutput.1").

                I've also tried
                String[] record = value.split ("\056");
                and it returned an array with zero length.

                Can anyone tell me what I'm doing wrong?
                String[] record = value.split ("\\.");

                The argument for split() method should be a regular expression.
                Your "\056" is not.
                • 5. Re: Using String.split() with '.' as a delimiter
                  807606
                  I'm trying to parse a string with the . as the
                  delimiter and am not having any luck.
                  String value = new String ("radioOutput.1");
                  
                  String[] record = value.split ("\0056");
                  should fill record[0] with "radioOutput" and
                  record[1] with "1", but I'm only getting a single
                  String back that contains the entire starting
                  String
                  ("radioOutput.1").

                  I've also tried
                  String[] record = value.split ("\056");
                  and it returned an array with zero length.

                  Can anyone tell me what I'm doing wrong?
                  Hmm... You are trying to split an String on a
                  delimeter, and the only character in the String IS
                  the delimeter. Therefore, there is nothing else in
                  the String, so it returns an empty array.

                  - Adam
                  Sorry, I must not have made it clear in my first post, but the second code section was just a replacement of the String.split() line. In other words:
                  String value = new String ("radioOutput.1");
                  String[] record = value.split ("\0056");
                  returns the entire original string in a single array element of record.

                  If I change the \0056 to \056, as in:
                  String value = new String ("radioOutput.1");
                  String[] record = value.split ("\056");
                  I get an empty array.
                  • 6. Re: Using String.split() with '.' as a delimiter
                    796125
                    Okay, I assume that you KNOW that \005 and \056 are escaped character codes, so \005 is not a string of length 4, but rather, a single character with character code 5 (which you see as a spade), and \056 is a single character, with code 56, which you see as a "." Now, splitting on the "." (which is EXACTLY equal to \056) will turn the string into a list of strings that were contained in the original, but were separated by "."

                    In the first string, there is no "." Therefore, there is just one string returned.

                    In the second string, there IS a "." and furthermore, it is the only character in the string. So when you split on the "." you remove the "." and have NOTHING left, so the size of the returned array is 0.

                    - Adam
                    • 7. Re: Using String.split() with '.' as a delimiter
                      796125
                      The argument for split() method should be a regular
                      expression.
                      Your "\056" is not.
                      Actually, it is. With a single \ java treats this as an escaped character AT COMPILE TIME. So the actual regular expression is "." (which is a valid regular expression)
                      • 8. Re: Using String.split() with '.' as a delimiter
                        800351
                        The argument for split() method should be a
                        regular
                        expression.
                        Your "\056" is not.
                        Actually, it is. With a single \ java treats this as
                        an escaped character AT COMPILE TIME. So the actual
                        regular expression is "." (which is a valid regular
                        expression)
                        You are absolutely wrong!!!!!
                        The valid regex here should be "\\.", which escapes a dot with a backslash.
                        • 9. Re: Using String.split() with '.' as a delimiter
                          796125
                          You are absolutely wrong!!!!!
                          The valid regex here should be "\\.", which escapes a
                          dot with a backslash.
                          That depends what you want your regex to do! "\\." and "." are both valid regular expressions - its just that one has a different behavior than the other.

                          - Adam
                          • 10. Re: Using String.split() with '.' as a delimiter
                            800351
                            The word 'valid' in your sense can be applied to any Java String.
                            But practically, "." can't be a valid regular expression as an argument for the split() method which requires specification of a delimiter. Because "." matches any single character, it can't work as delimiter to retrieve substrings of a string. So it is invalid in the context of this forum thread.
                            • 11. Re: Using String.split() with '.' as a delimiter
                              796125
                              The word 'valid' in your sense can be applied to any
                              Java String.
                              But practically, "." can't be a valid regular
                              expression as an argument for the split() method
                              which requires specification of a delimiter. Because
                              "." matches any single character, it can't work as
                              delimiter to retrieve substrings of a string. So it
                              is invalid in the context of this forum thread.
                              You're correct, and the key word here is "practically".

                              However, The String.split method does accept the string "."

                              I would say that the behavior is undefined.

                              At any rate, I think I understand the original problem, now.

                              If I had to guess, the OP was trying to use the escaped character code to create the "." so that it wouldn't be treated as matching any character? Unfortunately, the trick with java is that the compiler preprocesses all escaped character codes before parsing, so when it compiles, it doesn't know that it was an escaped character code instead of a "."

                              The solution (as previously mentioned) would, in this case, be to use "\\." (however, I stick to my previous statement that "." is a perfectly valid regex, which is accepted by the String.split method, and perhaps has undefined behavior therein).
                              • 12. Re: Using String.split() with '.' as a delimiter
                                796440
                                The word 'valid' in your sense can be applied to any
                                Java String.
                                No, not all strings are valid regexes.
                                • 13. Re: Using String.split() with '.' as a delimiter
                                  796440
                                  However, The String.split method does accept the
                                  string "."

                                  I would say that the behavior is undefined.
                                  It's perfectly well defined. It's just not intuitive. :-)
                                  • 14. Re: Using String.split() with '.' as a delimiter
                                    807606
                                    Okay, Okay...

                                    I can put an end to this.

                                    I tried:
                                    String value = new String ("radioOutput.1");
                                     
                                    String[] record = value.split ("\\.");
                                    and it worked as I want it to, now.

                                    Thank you all for the input.
                                    1 2 Previous Next