What's the best way to check if a String represents an integer in Java?

后端 未结 30 2170
野趣味
野趣味 2020-11-22 05:45

I normally use the following idiom to check if a String can be converted to an integer.

public boolean isInteger( String input ) {
    try {
        Integer.         


        
30条回答
  •  野的像风
    2020-11-22 06:38

    If you want to check if the string represents an integer that fits in an int type, I did a little modification to the jonas' answer, so that strings that represent integers bigger than Integer.MAX_VALUE or smaller than Integer.MIN_VALUE, will now return false. For example: "3147483647" will return false because 3147483647 is bigger than 2147483647, and likewise, "-2147483649" will also return false because -2147483649 is smaller than -2147483648.

    public static boolean isInt(String s) {
      if(s == null) {
        return false;
      }
      s = s.trim(); //Don't get tricked by whitespaces.
      int len = s.length();
      if(len == 0) {
        return false;
      }
      //The bottom limit of an int is -2147483648 which is 11 chars long.
      //[note that the upper limit (2147483647) is only 10 chars long]
      //Thus any string with more than 11 chars, even if represents a valid integer, 
      //it won't fit in an int.
      if(len > 11) {
        return false;
      }
      char c = s.charAt(0);
      int i = 0;
      //I don't mind the plus sign, so "+13" will return true.
      if(c == '-' || c == '+') {
        //A single "+" or "-" is not a valid integer.
        if(len == 1) {
          return false;
        }
        i = 1;
      }
      //Check if all chars are digits
      for(; i < len; i++) {
        c = s.charAt(i);
        if(c < '0' || c > '9') {
          return false;
        }
      }
      //If we reached this point then we know for sure that the string has at
      //most 11 chars and that they're all digits (the first one might be a '+'
      // or '-' thought).
      //Now we just need to check, for 10 and 11 chars long strings, if the numbers
      //represented by the them don't surpass the limits.
      c = s.charAt(0);
      char l;
      String limit;
      if(len == 10 && c != '-' && c != '+') {
        limit = "2147483647";
        //Now we are going to compare each char of the string with the char in
        //the limit string that has the same index, so if the string is "ABC" and
        //the limit string is "DEF" then we are gonna compare A to D, B to E and so on.
        //c is the current string's char and l is the corresponding limit's char
        //Note that the loop only continues if c == l. Now imagine that our string
        //is "2150000000", 2 == 2 (next), 1 == 1 (next), 5 > 4 as you can see,
        //because 5 > 4 we can guarantee that the string will represent a bigger integer.
        //Similarly, if our string was "2139999999", when we find out that 3 < 4,
        //we can also guarantee that the integer represented will fit in an int.
        for(i = 0; i < len; i++) {
          c = s.charAt(i);
          l = limit.charAt(i);
          if(c > l) {
            return false;
          }
          if(c < l) {
            return true;
          }
        }
      }
      c = s.charAt(0);
      if(len == 11) {
        //If the first char is neither '+' nor '-' then 11 digits represent a 
        //bigger integer than 2147483647 (10 digits).
        if(c != '+' && c != '-') {
          return false;
        }
        limit = (c == '-') ? "-2147483648" : "+2147483647";
        //Here we're applying the same logic that we applied in the previous case
        //ignoring the first char.
        for(i = 1; i < len; i++) {
          c = s.charAt(i);
          l = limit.charAt(i);
          if(c > l) {
            return false;
          }
          if(c < l) {
            return true;
          }
        }
      }
      //The string passed all tests, so it must represent a number that fits
      //in an int...
      return true;
    }
    

提交回复
热议问题