code: private static int toInteger(CharSequence seq) {
      int len = seq.length();
      int n = 0;
      int i = 0;
      while (i < len) {
        char c = seq.charAt(i);
        if (c >= '0' && c <= '9') {
          n *= 10;
          n += (int)(c - '0');
        } else {
          break;
        }
        i++;
      }
      return n;
    }
docs: Quick string to integer conversion.