5 Replies Latest reply: Feb 18, 2011 10:50 AM by baftos RSS

    Splitting a string when an integer is reached

    802561
      Hi,

      I'm trying to figure out how, if possible, to split a string when an integer comes in the strong. What I'm doing is reading from a web page and the output is "1930Stella" as an example. What I want to do is insert those into a table, so it's like this:
      String     |     Integer
       Stella           1930
      I'm not sure how I can go about doing this, please help!
        • 1. Re: Splitting a string when an integer is reached
          802561
          Nevermind, I've figured it out after some testing.. here's my solution if anyone is interested (can't believe I didn't try this sooner):
          public String parseString(boolean numbers, String string) {
               StringBuffer stringBuffer = new StringBuffer();
               char c;
               for (int i = 0; i < string.length(); i++) {
                        c = string.charAt(i);
                        if (numbers && Character.isDigit(c)) {
                             stringBuffer.append(c);
                        } else if (!numbers && !Character.isDigit(c)) {
                             stringBuffer.append(c);
                        }
               }
               return stringBuffer.toString();
          }
          • 2. Re: Splitting a string when an integer is reached
            804091
            Just a thought...

            Why not make this function return something like this:
            >
            "1930Stella" -> parseString -> "1930|Stella"
            >

            You're obviously halfway there, but right now if you want to get both the number and the string out of the input, you have to loop over the input twice. Why not just loop over the input once, insert a delimiter between your 'fields', and call split on the resulting string later? Then again, you know the overall requirements for the issue at hand.

            Also, is the input string always a number followed by characters (i.e. 1930Stella), or can it be something ridiculous like "1234abc56d7e8"? I only ask because right now your method only handles the first case nicely, on the assumption that your result will always be "number followed by characters".
            • 3. Re: Splitting a string when an integer is reached
              YoungWinston
              aeternaly wrote:
              I'm trying to figure out how, if possible, to split a string when an integer comes in the strong. What I'm doing is reading from a web page and the output is "1930Stella" as an example.
              Another wrinkle for you. Could it ever be "-1930Stella"?

              Winston
              • 4. Re: Splitting a string when an integer is reached
                802561
                YoungWinston wrote:
                aeternaly wrote:
                I'm trying to figure out how, if possible, to split a string when an integer comes in the strong. What I'm doing is reading from a web page and the output is "1930Stella" as an example.
                Another wrinkle for you. Could it ever be "-1930Stella"?

                Winston
                No, it can't ever be anything other than "1930Stella" or "Stella1930", but it will always be integerString in the format that I'm reading from the site.
                almightywiz wrote:
                Just a thought...

                Why not make this function return something like this:
                >
                "1930Stella" -> parseString -> "1930|Stella"
                >

                You're obviously halfway there, but right now if you want to get both the number and the string out of the input, you have to loop over the input twice. Why not just loop over the input once, insert a delimiter between your 'fields', and call split on the resulting string later? Then again, you know the overall requirements for the issue at hand.

                Also, is the input string always a number followed by characters (i.e. 1930Stella), or can it be something ridiculous like "1234abc56d7e8"? I only ask because right now your method only handles the first case nicely, on the assumption that your result will always be "number followed by characters".
                As I said above, the method works for what I need (only integerString/stringInteger format). I was looking at the delimiter idea but couldn't quite fully understand it. I'll give it another go, though!
                • 5. Re: Splitting a string when an integer is reached
                  baftos
                  static String[] splitDigitsRest(String s)
                  {
                    return new String[] {s.replaceAll("\\D",""), s.replaceAll("\\d","")};
                  }