1 2 Previous Next 22 Replies Latest reply: Oct 16, 2007 12:43 PM by 796125 RSS

    string parse

    807600
      Quick question:

      Image i have a string that looks like this:

      "------058-JLP3"

      I want to remove the leading dashes to result in "058-JLP3". However, the amount of leading dashes is variable. What is the easiest way to do this?
        • 1. Re: string parse
          807600
          there are a number of ways to do it:

          1. You could form a regular expression on your string and replace the match with ""
          2. You could go through each character in the String using (charAt()) and a for-loop, and remove the character if you want
          3. you could find the position of the last "-" and do a substring() call from that position + 1 to obtain the trimmed string
          4. You could do a while loop ( while (string.charAt(0) == '-') string = string.substring(1);

          The regex way:
          string = string.replaceFirst("[\-]*058\-JLP3", "");
          • 2. Re: string parse
            796125
            You can use a simple regular expression to do this:
            String.replaceAll( "^-+(?=[^\\-])", "");
            - Adam
            • 3. Re: string parse
              807600
              Thanks.
              • 4. Re: string parse
                796125
                The regex way:
                string = string.replaceFirst("[\-]*058\-JLP3", "");
                Your Regex will only match if the string contains exactly "058-JLP3". I suspect that this isn't the case, because if it was, there wouldn't be a point in trying to extract it.

                - Adam
                • 5. Re: string parse
                  807600
                  About my post above, I tried to edit it but it didn't let me.

                  Anyway, your regex couldn't be what the poster above did, because it will take out the middle dash, too.

                  you could try:
                  string = string.replaceFirst("([\\-]*)058\\-JLP3", "058-JLP3");
                  • 6. Re: string parse
                    807600
                    guitar_man_F wrote:

                    Your Regex will only match if the string contains exactly "058-JLP3". I suspect that this isn't the case, because if it was, there wouldn't be a point in trying to extract it.

                    - Adam
                    Lol, a valid point. Neither of our ways would work as it is now. You'd need to do a regex replace based on groupings, as I said before.
                    • 7. Re: string parse
                      796125
                      2. You could go through each character in the String using (charAt()) and a for-loop, and remove the character if you want
                      If you go this route, you want to make sure that the first time you encounter a non-hyphen character, you exit the loop.
                      • 8. Re: string parse
                        796125
                        3. you could find the position of the last "-" and do a substring() call from that position + 1 to obtain the trimmed string
                        Really, when you say last, you mean the last of the leading hyphens, because the actual last hyphen is in the middle of 058-JLP3, which you don't want to remove. So really, this boils down to the same as #2 and #4 (which are basically identical), except that you don't actually do the 'substring' operation until you've found the first non-hyphen character
                        • 9. Re: string parse
                          807600
                          I'd rather not waste time going through a for loop. I like the idea of a regular expression. With the regex, I want to only replace the leading dashes, and I should have mentioned the following data is variable. So, basically all I want to do is remove a variable amount of leading dashes from a string of data without removing any dashes that may be present in the data portion. So, a regex that gets me from -------data-data to data-data

                          Thanks
                          • 10. Re: string parse
                            807600
                            Oh, you know what--I'm just plain wrong. Didn't see your "^" in your regex, didn't even read your regex actually. I take back everything I've said this thread. Not my finest hour.

                            (grabs collar nervously).
                            • 11. Re: string parse
                              796125
                              Lol, a valid point. Neither of our ways would work as it is now. You'd need to do a regex replace based on groupings, as I said before.
                              Actually, I don't see what's wrong with the way I suggested. My regex doesn't assume the value in the string, and using the forward look-ahead means that the part matched by the look-ahead won't be replaced. I did test my regex before I posted it. It works.

                              Having said that, you're correct. You could do a regex replace based on groupings. I don't think you can use methods from String to do it, but I suspect you can do it using Matcher. I haven't actually ever tried, though.

                              - Adam
                              • 12. Re: string parse
                                796125
                                endasil wrote:
                                Oh, you know what--I'm just plain wrong. Didn't see your "^" in your regex, didn't even read your regex actually. I take back everything I've said this thread. Not my finest hour.

                                (grabs collar nervously).
                                That's okay. Happens to everybody. To me, more than most, too.

                                - Adam
                                • 13. Re: string parse
                                  807600
                                  Will

                                  String.replaceAll( "-+(?=[-])", "");

                                  that only replace leading dashes?
                                  • 14. Re: string parse
                                    796125
                                    newbie_josh_ee wrote:
                                    I'd rather not waste time going through a for loop. I like the idea of a regular expression. With the regex, I want to only replace the leading dashes, and I should have mentioned the following data is variable. So, basically all I want to do is remove a variable amount of leading dashes from a string of data without removing any dashes that may be present in the data portion. So, a regex that gets me from -------data-data to data-data

                                    Thanks
                                    Take another look at this one, then:
                                    String.replaceAll( "^-+(?=[^-])", "");
                                    You don't need the \ \ I had in there before. That was a typo (although it did still work).

                                    If you're not familiar with Regex's, here's an explanation of how this works:

                                    The leading ^ indicates that it will start matching the very first character of the string. Then the -+ indicates that it will match an sequence of - of unlimited length. The ( ? = [ ^  - ] ) is called a forward look-ahead. The purpose is to say, stop matching characters as soon as you find a character that is not a hyphen. The ( ? = ) part is syntactic sugar for a forward look-ahead. The [ ^  - ] tells it to match any character except hyphen.

                                    So the net result is that the regular expression will match then entire string of hyphens at the beginning of the string. It then replaces them with the empty string.

                                    - Adam
                                    1 2 Previous Next