繁体   English   中英

在 Java 中检查 String 是否代表整数的最佳方法是什么?

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

我通常使用以下习语来检查字符串是否可以转换为整数。

public boolean isInteger( String input ) {
    try {
        Integer.parseInt( input );
        return true;
    }
    catch( Exception e ) {
        return false;
    }
}

是我一个人,还是这看起来有点骇人听闻? 什么是更好的方法?


请参阅我的答案(基于CodingWithSpike较早的答案的基准测试)以了解为什么我改变了立场并接受了Jonas Klemming对这个问题的回答 我认为这个原始代码会被大多数人使用,因为它实现更快,更易于维护,但是当提供非整数数据时,它会慢几个数量级。

如果您不关心潜在的溢出问题,此函数的执行速度将比使用Integer.parseInt()快 20-30 倍。

public static boolean isInteger(String str) {
    if (str == null) {
        return false;
    }
    int length = str.length();
    if (length == 0) {
        return false;
    }
    int i = 0;
    if (str.charAt(0) == '-') {
        if (length == 1) {
            return false;
        }
        i = 1;
    }
    for (; i < length; i++) {
        char c = str.charAt(i);
        if (c < '0' || c > '9') {
            return false;
        }
    }
    return true;
}

你有它,但你应该只捕获NumberFormatException

做了一个快速的基准测试。 异常实际上并不那么昂贵,除非您开始弹出多个方法并且 JVM 必须做很多工作才能使执行堆栈就位。 当保持相同的方法时,他们的表现并不差。

 public void RunTests()
 {
     String str = "1234567890";

     long startTime = System.currentTimeMillis();
     for(int i = 0; i < 100000; i++)
         IsInt_ByException(str);
     long endTime = System.currentTimeMillis();
     System.out.print("ByException: ");
     System.out.println(endTime - startTime);

     startTime = System.currentTimeMillis();
     for(int i = 0; i < 100000; i++)
         IsInt_ByRegex(str);
     endTime = System.currentTimeMillis();
     System.out.print("ByRegex: ");
     System.out.println(endTime - startTime);

     startTime = System.currentTimeMillis();
     for(int i = 0; i < 100000; i++)
         IsInt_ByJonas(str);
     endTime = System.currentTimeMillis();
     System.out.print("ByJonas: ");
     System.out.println(endTime - startTime);
 }

 private boolean IsInt_ByException(String str)
 {
     try
     {
         Integer.parseInt(str);
         return true;
     }
     catch(NumberFormatException nfe)
     {
         return false;
     }
 }

 private boolean IsInt_ByRegex(String str)
 {
     return str.matches("^-?\\d+$");
 }

 public boolean IsInt_ByJonas(String str)
 {
     if (str == null) {
             return false;
     }
     int length = str.length();
     if (length == 0) {
             return false;
     }
     int i = 0;
     if (str.charAt(0) == '-') {
             if (length == 1) {
                     return false;
             }
             i = 1;
     }
     for (; i < length; i++) {
             char c = str.charAt(i);
             if (c <= '/' || c >= ':') {
                     return false;
             }
     }
     return true;
 }

输出:

通过异常:31

ByRegex:453(注意:每次都重新编译模式)

乔纳斯:16

我同意 Jonas K 的解决方案也是最强大的。 看起来他赢了:)

org.apache.commons.lang.StringUtils.isNumeric 

尽管 Java 的标准库确实缺少这样的实用程序函数

我认为 Apache Commons 是每个 Java 程序员的“必备”

太糟糕了它还没有移植到 Java5

因为有可能人们仍然访问这里并且在基准测试之后会对 Regex 产生偏见......所以我将提供基准的更新版本,以及 Regex 的编译版本。 与之前的基准测试相反,这个测试表明 Regex 解决方案实际上始终具有良好的性能。

从蜥蜴比尔复制并更新为编译版本:

private final Pattern pattern = Pattern.compile("^-?\\d+$");

public void runTests() {
    String big_int = "1234567890";
    String non_int = "1234XY7890";

    long startTime = System.currentTimeMillis();
    for(int i = 0; i < 100000; i++)
            IsInt_ByException(big_int);
    long endTime = System.currentTimeMillis();
    System.out.print("ByException - integer data: ");
    System.out.println(endTime - startTime);

    startTime = System.currentTimeMillis();
    for(int i = 0; i < 100000; i++)
            IsInt_ByException(non_int);
    endTime = System.currentTimeMillis();
    System.out.print("ByException - non-integer data: ");
    System.out.println(endTime - startTime);

    startTime = System.currentTimeMillis();
    for(int i = 0; i < 100000; i++)
            IsInt_ByRegex(big_int);
    endTime = System.currentTimeMillis();
    System.out.print("\nByRegex - integer data: ");
    System.out.println(endTime - startTime);

    startTime = System.currentTimeMillis();
    for(int i = 0; i < 100000; i++)
            IsInt_ByRegex(non_int);
    endTime = System.currentTimeMillis();
    System.out.print("ByRegex - non-integer data: ");
    System.out.println(endTime - startTime);

    startTime = System.currentTimeMillis();
    for (int i = 0; i < 100000; i++)
            IsInt_ByCompiledRegex(big_int);
    endTime = System.currentTimeMillis();
    System.out.print("\nByCompiledRegex - integer data: ");
    System.out.println(endTime - startTime);

    startTime = System.currentTimeMillis();
    for (int i = 0; i < 100000; i++)
            IsInt_ByCompiledRegex(non_int);
    endTime = System.currentTimeMillis();
    System.out.print("ByCompiledRegex - non-integer data: ");
    System.out.println(endTime - startTime);


    startTime = System.currentTimeMillis();
    for(int i = 0; i < 100000; i++)
            IsInt_ByJonas(big_int);
    endTime = System.currentTimeMillis();
    System.out.print("\nByJonas - integer data: ");
    System.out.println(endTime - startTime);

    startTime = System.currentTimeMillis();
    for(int i = 0; i < 100000; i++)
            IsInt_ByJonas(non_int);
    endTime = System.currentTimeMillis();
    System.out.print("ByJonas - non-integer data: ");
    System.out.println(endTime - startTime);
}

private boolean IsInt_ByException(String str)
{
    try
    {
        Integer.parseInt(str);
        return true;
    }
    catch(NumberFormatException nfe)
    {
        return false;
    }
}

private boolean IsInt_ByRegex(String str)
{
    return str.matches("^-?\\d+$");
}

private boolean IsInt_ByCompiledRegex(String str) {
    return pattern.matcher(str).find();
}

public boolean IsInt_ByJonas(String str)
{
    if (str == null) {
            return false;
    }
    int length = str.length();
    if (length == 0) {
            return false;
    }
    int i = 0;
    if (str.charAt(0) == '-') {
            if (length == 1) {
                    return false;
            }
            i = 1;
    }
    for (; i < length; i++) {
            char c = str.charAt(i);
            if (c <= '/' || c >= ':') {
                    return false;
            }
    }
    return true;
}

结果:

ByException - integer data: 45
ByException - non-integer data: 465

ByRegex - integer data: 272
ByRegex - non-integer data: 131

ByCompiledRegex - integer data: 45
ByCompiledRegex - non-integer data: 26

ByJonas - integer data: 8
ByJonas - non-integer data: 2

这部分取决于您所说的“可以转换为整数”是什么意思。

如果您的意思是“可以在 Java 中转换为 int”,那么 Jonas 的答案是一个好的开始,但并没有完全完成工作。 例如,它将通过 999999999999999999999999999999。 我会在方法的末尾添加来自您自己的问题的正常 try/catch 调用。

逐个字符的检查将有效地拒绝“根本不是整数”的情况,留下“它是一个整数但 Java 无法处理它”的情况由较慢的异常路由捕获。 可以这样做有点太的手,但是这将是一个复杂得多

只有一条关于正则表达式的评论。 这里提供的每个例子都是错误的!。 如果您想使用正则表达式,请不要忘记编译模式需要很多时间。 这个:

str.matches("^-?\\d+$")

还有这个:

Pattern.matches("-?\\d+", input);

导致在每个方法调用中编译模式。 要正确使用它,请遵循:

import java.util.regex.Pattern;

/**
 * @author Rastislav Komara
 */
public class NaturalNumberChecker {
    public static final Pattern PATTERN = Pattern.compile("^\\d+$");

    boolean isNaturalNumber(CharSequence input) {
        return input != null && PATTERN.matcher(input).matches();
    }
}

有番石榴版本:

import com.google.common.primitives.Ints;

Integer intValue = Ints.tryParse(stringValue);

如果解析字符串失败,它将返回 null 而不是抛出异常。

我从rally25rs 的答案中复制了代码,并为非整数数据添加了一些测试。 不可否认,结果支持 Jonas Klemming 发布的方法。 当你有整数数据时,我最初发布的 Exception 方法的结果非常好,但当你没有时,它们是最糟糕的,而 RegEx 解决方案的结果(我敢打赌很多人都在使用)一直很糟糕。 有关编译的正则表达式示例,请参阅Felipe 的答案,该示例要快得多。

public void runTests()
{
    String big_int = "1234567890";
    String non_int = "1234XY7890";

    long startTime = System.currentTimeMillis();
    for(int i = 0; i < 100000; i++)
        IsInt_ByException(big_int);
    long endTime = System.currentTimeMillis();
    System.out.print("ByException - integer data: ");
    System.out.println(endTime - startTime);

    startTime = System.currentTimeMillis();
    for(int i = 0; i < 100000; i++)
        IsInt_ByException(non_int);
    endTime = System.currentTimeMillis();
    System.out.print("ByException - non-integer data: ");
    System.out.println(endTime - startTime);

    startTime = System.currentTimeMillis();
    for(int i = 0; i < 100000; i++)
        IsInt_ByRegex(big_int);
    endTime = System.currentTimeMillis();
    System.out.print("\nByRegex - integer data: ");
    System.out.println(endTime - startTime);

    startTime = System.currentTimeMillis();
    for(int i = 0; i < 100000; i++)
        IsInt_ByRegex(non_int);
    endTime = System.currentTimeMillis();
    System.out.print("ByRegex - non-integer data: ");
    System.out.println(endTime - startTime);

    startTime = System.currentTimeMillis();
    for(int i = 0; i < 100000; i++)
        IsInt_ByJonas(big_int);
    endTime = System.currentTimeMillis();
    System.out.print("\nByJonas - integer data: ");
    System.out.println(endTime - startTime);

    startTime = System.currentTimeMillis();
    for(int i = 0; i < 100000; i++)
        IsInt_ByJonas(non_int);
    endTime = System.currentTimeMillis();
    System.out.print("ByJonas - non-integer data: ");
    System.out.println(endTime - startTime);
}

private boolean IsInt_ByException(String str)
{
    try
    {
        Integer.parseInt(str);
        return true;
    }
    catch(NumberFormatException nfe)
    {
        return false;
    }
}

private boolean IsInt_ByRegex(String str)
{
    return str.matches("^-?\\d+$");
}

public boolean IsInt_ByJonas(String str)
{
    if (str == null) {
            return false;
    }
    int length = str.length();
    if (length == 0) {
            return false;
    }
    int i = 0;
    if (str.charAt(0) == '-') {
            if (length == 1) {
                    return false;
            }
            i = 1;
    }
    for (; i < length; i++) {
            char c = str.charAt(i);
            if (c <= '/' || c >= ':') {
                    return false;
            }
    }
    return true;
}

结果:

ByException - integer data: 47
ByException - non-integer data: 547

ByRegex - integer data: 390
ByRegex - non-integer data: 313

ByJonas - integer data: 0
ByJonas - non-integer data: 16

您可以使用字符串类的matches 方法。 [0-9] 表示它可以是的所有值,+ 表示它必须至少有一个字符长,而 * 表示它可以是零个或多个字符长。

boolean isNumeric = yourString.matches("[0-9]+"); // 1 or more characters long, numbers only
boolean isNumeric = yourString.matches("[0-9]*"); // 0 or more characters long, numbers only

这更短,但更短不一定更好(并且它不会捕获超出范围的整数值, 如 dantel 的评论中指出的那样):

input.matches("^-?\\d+$");

就个人而言,由于实现是在辅助方法中隐藏的,并且正确性胜过长度,所以我只会使用类似于您所拥有的东西(减去捕获基本Exception类而不是NumberFormatException )。

怎么样:

return Pattern.matches("-?\\d+", input);

这是 Jonas Klemming 答案的 Java 8 变体:

public static boolean isInteger(String str) {
    return str != null && str.length() > 0 &&
         IntStream.range(0, str.length()).allMatch(i -> i == 0 && (str.charAt(i) == '-' || str.charAt(i) == '+')
                  || Character.isDigit(str.charAt(i)));
}

测试代码:

public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    Arrays.asList("1231231", "-1232312312", "+12313123131", "qwqe123123211", "2", "0000000001111", "", "123-", "++123",
            "123-23", null, "+-123").forEach(s -> {
        System.out.printf("%15s %s%n", s, isInteger(s));
    });
}

测试代码结果:

        1231231 true
    -1232312312 true
   +12313123131 true
  qwqe123123211 false
              2 true
  0000000001111 true
                false
           123- false
          ++123 false
         123-23 false
           null false
          +-123 false

您只需检查NumberFormatException :-

 String value="123";
 try  
 {  
    int s=Integer.parseInt(any_int_val);
    // do something when integer values comes 
 }  
 catch(NumberFormatException nfe)  
 {  
          // do something when string values comes 
 }  

如果您的字符串数组包含纯整数和字符串,下面的代码应该可以工作。 你只需要看第一个字符。 例如 ["4","44","abc","77","bond"]

if (Character.isDigit(string.charAt(0))) {
    //Do something with int
}

您还可以使用Scanner类,并使用hasNextInt() - 这也允许您测试其他类型,如浮点数等。

你可以试试 apache utils

NumberUtils.isCreatable(myText)

请参阅此处的 javadoc

另外一个选项:

private boolean isNumber(String s) {
    boolean isNumber = true;
    for (char c : s.toCharArray()) {
        isNumber = isNumber && Character.isDigit(c);
    }
    return isNumber;
}

如果要检查字符串是否表示适合 int 类型的整数,我对 jonas 的答案做了一些修改,以便表示大于 Integer.MAX_VALUE 或小于 Integer.MIN_VALUE 的整数的字符串现在将返回错误的。 例如:"3147483647" 将返回 false,因为 3147483647 大于 2147483647,同样,"-2147483649" 也会返回 false,因为 -2147483649 小于 -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;
}

您可能还需要考虑用例:

如果大多数时候您希望数字有效,那么捕获异常只会在尝试转换无效数字时导致性能开销。 而调用一些isInteger()方法然后使用Integer.parseInt()进行转换总是会导致有效数字的性能开销 - 字符串被解析两次,一次通过检查,一次通过转换。

这是对Jonas代码的修改,用于检查字符串是否在要转换为整数的范围内。

public static boolean isInteger(String str) {
    if (str == null) {
        return false;
    }
    int length = str.length();
    int i = 0;

    // set the length and value for highest positive int or lowest negative int
    int maxlength = 10;
    String maxnum = String.valueOf(Integer.MAX_VALUE);
    if (str.charAt(0) == '-') { 
        maxlength = 11;
        i = 1;
        maxnum = String.valueOf(Integer.MIN_VALUE);
    }  

    // verify digit length does not exceed int range
    if (length > maxlength) { 
        return false; 
    }

    // verify that all characters are numbers
    if (maxlength == 11 && length == 1) {
        return false;
    }
    for (int num = i; num < length; num++) {
        char c = str.charAt(num);
        if (c < '0' || c > '9') {
            return false;
        }
    }

    // verify that number value is within int range
    if (length == maxlength) {
        for (; i < length; i++) {
            if (str.charAt(i) < maxnum.charAt(i)) {
                return true;
            }
            else if (str.charAt(i) > maxnum.charAt(i)) {
                return false;
            }
        }
    }
    return true;
}

我相信遇到异常的风险为零,因为正如您在下面看到的那样,您总是可以安全地将int解析为String而不是相反。

所以:

  1. 检查字符串中的每个字符槽是否至少匹配字符{"0","1","2","3","4","5","6","7", "8","9"}

     if(aString.substring(j, j+1).equals(String.valueOf(i)))
  2. 您将在插槽中遇到的所有时间与上述字符相加

     digits++;
  3. 最后你检查你遇到整数作为字符的次数是否等于给定字符串的长度。

     if(digits == aString.length())

在实践中,我们有:

    String aString = "1234224245";
    int digits = 0;//count how many digits you encountered
    for(int j=0;j<aString.length();j++){
        for(int i=0;i<=9;i++){
            if(aString.substring(j, j+1).equals(String.valueOf(i)))
                    digits++;
        }
    }
    if(digits == aString.length()){
        System.out.println("It's an integer!!");
        }
    else{
        System.out.println("It's not an integer!!");
    }
    
    String anotherString = "1234f22a4245";
    int anotherDigits = 0;//count how many digits you encountered
    for(int j=0;j<anotherString.length();j++){
        for(int i=0;i<=9;i++){
            if(anotherString.substring(j, j+1).equals(String.valueOf(i)))
                    anotherDigits++;
        }
    }
    if(anotherDigits == anotherString.length()){
        System.out.println("It's an integer!!");
        }
    else{
        System.out.println("It's not an integer!!");
    }

结果是:

是整数!!

不是整数!!

同样,您可以验证Stringfloat还是double float ,但在这些情况下,您只需要遇到一个 . (点)在字符串中,当然检查digits == (aString.length()-1)

同样,这里遇到解析异常的风险为零,但是如果您计划解析一个已知包含数字(假设是int数据类型)的字符串,您必须首先检查它是否适合该数据类型。 否则你必须施放它。

我希望我有所帮助

如果您使用的是 Android API,则可以使用:

TextUtils.isDigitsOnly(str);
is_number = true;
try {
  Integer.parseInt(mystr)
} catch (NumberFormatException  e) {
  is_number = false;
}

您所做的工作有效,但您可能不应该总是那样检查。 抛出异常应保留用于“异常”情况(尽管可能适合您的情况),并且在性能方面成本非常高。

Number number;
try {
    number = NumberFormat.getInstance().parse("123");
} catch (ParseException e) {
    //not a number - do recovery.
    e.printStackTrace();
}
//use number

这对我有用。 简单地识别一个字符串是原始值还是数字。

private boolean isPrimitive(String value){
        boolean status=true;
        if(value.length()<1)
            return false;
        for(int i = 0;i<value.length();i++){
            char c=value.charAt(i);
            if(Character.isDigit(c) || c=='.'){

            }else{
                status=false;
                break;
            }
        }
        return status;
    }

要检查所有 int 字符,您可以简单地使用双重否定。

如果 (!searchString.matches("[^0-9]+$")) ...

[^0-9]+$ 检查是否有任何非整数字符,因此如果为真则测试失败。 只是不是那样,你就会成功。

找到这个可能有帮助:

public static boolean isInteger(String self) {
    try {
        Integer.valueOf(self.trim());
        return true;
    } catch (NumberFormatException nfe) {
        return false;
    }
}

我在这里看到了很多答案,但他们中的大多数都能够确定字符串是否为数字,但是他们无法检查数字是否在整数范围内......

因此,我的目的是这样的:

public static boolean isInteger(String str) {
    if (str == null || str.isEmpty()) {
        return false;
    }
    try {
        long value = Long.valueOf(str);
        return value >= -2147483648 && value <= 2147483647;
    } catch (Exception ex) {
        return false;
    }
}

当解释比表现更重要时

我注意到很多讨论都集中在某些解决方案的效率上,但没有讨论为什么字符串不是整数。 此外,每个人似乎都认为数字“2.00”不等于“2”。 从数学和人类的角度来说,它们平等的(即使计算机科学说它们不是,并且有充分的理由)。 这就是为什么上面的“Integer.parseInt”解决方案很弱(取决于您的要求)。

无论如何,为了使软件更智能、更人性化,我们需要创建能够像我们一样思考并解释失败原因的软件。 在这种情况下:

public static boolean isIntegerFromDecimalString(String possibleInteger) {
possibleInteger = possibleInteger.trim();
try {
    // Integer parsing works great for "regular" integers like 42 or 13.
    int num = Integer.parseInt(possibleInteger);
    System.out.println("The possibleInteger="+possibleInteger+" is a pure integer.");
    return true;
} catch (NumberFormatException e) {
    if (possibleInteger.equals(".")) {
        System.out.println("The possibleInteger=" + possibleInteger + " is NOT an integer because it is only a decimal point.");
        return false;
    } else if (possibleInteger.startsWith(".") && possibleInteger.matches("\\.[0-9]*")) {
        if (possibleInteger.matches("\\.[0]*")) {
            System.out.println("The possibleInteger=" + possibleInteger + " is an integer because it starts with a decimal point and afterwards is all zeros.");
            return true;
        } else {
            System.out.println("The possibleInteger=" + possibleInteger + " is NOT an integer because it starts with a decimal point and afterwards is not all zeros.");
            return false;
        }
    } else if (possibleInteger.endsWith(".")  && possibleInteger.matches("[0-9]*\\.")) {
        System.out.println("The possibleInteger="+possibleInteger+" is an impure integer (ends with decimal point).");
        return true;
    } else if (possibleInteger.contains(".")) {
        String[] partsOfPossibleInteger = possibleInteger.split("\\.");
        if (partsOfPossibleInteger.length == 2) {
            //System.out.println("The possibleInteger=" + possibleInteger + " is split into '" + partsOfPossibleInteger[0] + "' and '" + partsOfPossibleInteger[1] + "'.");
            if (partsOfPossibleInteger[0].matches("[0-9]*")) {
                if (partsOfPossibleInteger[1].matches("[0]*")) {
                    System.out.println("The possibleInteger="+possibleInteger+" is an impure integer (ends with all zeros after the decimal point).");
                    return true;
                } else if (partsOfPossibleInteger[1].matches("[0-9]*")) {
                    System.out.println("The possibleInteger=" + possibleInteger + " is NOT an integer because it the numbers after the decimal point (" + 
                                partsOfPossibleInteger[1] + ") are not all zeros.");
                    return false;
                } else {
                    System.out.println("The possibleInteger=" + possibleInteger + " is NOT an integer because it the 'numbers' after the decimal point (" + 
                            partsOfPossibleInteger[1] + ") are not all numeric digits.");
                    return false;
                }
            } else {
                System.out.println("The possibleInteger=" + possibleInteger + " is NOT an integer because it the 'number' before the decimal point (" + 
                        partsOfPossibleInteger[0] + ") is not a number.");
                return false;
            }
        } else {
            System.out.println("The possibleInteger="+possibleInteger+" is NOT an integer because it has a strange number of decimal-period separated parts (" +
                    partsOfPossibleInteger.length + ").");
            return false;
        }
    } // else
    System.out.println("The possibleInteger='"+possibleInteger+"' is NOT an integer, even though it has no decimal point.");
    return false;
}
}

测试代码:

String[] testData = {"0", "0.", "0.0", ".000", "2", "2.", "2.0", "2.0000", "3.14159", ".0001", ".", "$4.0", "3E24", "6.0221409e+23"};
int i = 0;
for (String possibleInteger : testData ) {
    System.out.println("");
    System.out.println(i + ". possibleInteger='" + possibleInteger +"' isIntegerFromDecimalString=" + isIntegerFromDecimalString(possibleInteger));
    i++;
}

我不喜欢使用正则表达式的方法,因为正则表达式无法检查范围( Integer.MIN_VALUEInteger.MAX_VALUE )。

如果在大多数情况下你期望 int 值,而不是 int 是不常见的,那么我建议使用Integer.valueOfInteger.parseInt版本,并带有NumberFormatException捕获。 这种方法的优点 - 您的代码具有良好的可读性:

public static boolean isInt(String s) {
  try {
    Integer.parseInt(s);
    return true;
  } catch (NumberFormatException nfe) {
    return false;
  }
}

如果您需要检查 String 是否为整数,并关心性能,那么最好的方法是使用Integer.parseInt java jdk 实现,但几乎没有修改(用 return false 替换 throw):

此功能具有良好的性能并保证正确的结果:

   public static boolean isInt(String s) {
    int radix = 10;

    if (s == null) {
        return false;
    }

    if (radix < Character.MIN_RADIX) {
        return false;
    }

    if (radix > Character.MAX_RADIX) {
        return false;
    }

    int result = 0;
    boolean negative = false;
    int i = 0, len = s.length();
    int limit = -Integer.MAX_VALUE;
    int multmin;
    int digit;

    if (len > 0) {
        char firstChar = s.charAt(0);
        if (firstChar < '0') { // Possible leading "+" or "-"
            if (firstChar == '-') {
                negative = true;
                limit = Integer.MIN_VALUE;
            } else if (firstChar != '+')
                return false;

            if (len == 1) // Cannot have lone "+" or "-"
                return false;
            i++;
        }
        multmin = limit / radix;
        while (i < len) {
            // Accumulating negatively avoids surprises near MAX_VALUE
            digit = Character.digit(s.charAt(i++), radix);
            if (digit < 0) {
                return false;
            }
            if (result < multmin) {
                return false;
            }
            result *= radix;
            if (result < limit + digit) {
                return false;
            }
            result -= digit;
        }
    } else {
        return false;
    }
    return true;
}

你可以:

  1. 检查字符串是否为数字
  2. 检查是不是太长被解析为long
  3. 检查生成的long值是否小到可以用int表示

(假设您出于某种原因必须自己实现这一点:您可能应该首先查看com.google.common.primitives.Ints.tryParse(String)并看看这是否足以满足您的目的(如另一个回答))

// Credit to Rastislav Komara’s answer: https://stackoverflow.com/a/237895/1725151
private static final Pattern nonZero = Pattern.compile("^-?[1-9]\\d*$");

// See if `str` can be parsed as an `int` (does not trim)
// Strings like `0023` are rejected (leading zeros).
public static boolean parsableAsInt(@Nonnull String str) {
    if (str.isEmpty()) {
        return false;
    }
    if (str.equals("0")) {
        return true;
    }
    if (canParseAsLong(str)) {
        long value = Long.valueOf(str);
        if (value >= Integer.MIN_VALUE && value <= Integer.MAX_VALUE) {
            return true;
        }
    }
    return false;
}

private static boolean canParseAsLong(String str) {
    final int intMaxLength = 11;
    return str.length() <= intMaxLength && nonZero.matcher(str).matches();
}

此方法也可以转换为返回Optional<Integer>以便您不必在客户端代码中对字符串进行两次解析(一次是检查是否可能,第二次是“真正的”)。 例如:

if (canParseAsLong(str)) {
    long value = Long.valueOf(str);
    if (value >= Integer.MIN_VALUE && value <= Integer.MAX_VALUE) {
        return Optional.of((int) value);
    }
}

对@Jonas K anwser 的一点改进,这个函数将排除像"*"这样的单个运算符。

public boolean isInteger(String str) {
    // null pointer
    if (str == null) {
        return false;
    }
    int len = str.length();
    // empty string
    if (len == 0) {
        return false;
    }
    // one digit, cannot begin with 0
    if (len == 1) {
        char c = str.charAt(0);
        if ((c < '1') || (c > '9')) {
            return false;
        }
    }

    for (int i = 0; i < len; i++) {
        char c = str.charAt(i);
        // check positive, negative sign
        if (i == 0) {
            if (c == '-' || c == '+') {
                continue;
            }
        }
        // check each character matches [0-9]
        if ((c < '0') || (c > '9')) {
            return false;
        }
    }
    return true;
}

我最近(今天)需要想出一个快速的方法来做到这一点,当然,当肩膀上的猴子(良心)醒来时,我将使用异常方法来缓解这种情况,所以它把我带进了这个熟悉的老兔子洞; 没有例外并没有那么贵,事实上有时例外更快(旧的 AIX 多处理器系统),但不管它是否优雅,所以我做了一些我年轻时从未做过的事情,令我惊讶的是这里也没有人做过(如果有人做过,我道歉)错过了,老实说我没有找到):那么我认为我们都错过了什么? 看看 JRE 如何实现它,是的,他们抛出了一个异常,但我们总是可以跳过那部分。

10 年前年轻的我会觉得这在他之下,但他又是一个大嘴炫耀,脾气差,神情复杂,所以就是这样。

我把它放在这里是为了将来来这里的任何人。 这是我发现的:

public static int parseInt(String s, int radix) throws NumberFormatException
{
    /*
     * WARNING: This method may be invoked early during VM initialization
     * before IntegerCache is initialized. Care must be taken to not use
     * the valueOf method.
     */

    if (s == null) {
        throw new NumberFormatException("null");
    }

    if (radix < Character.MIN_RADIX) {
        throw new NumberFormatException("radix " + radix +
                                        " less than Character.MIN_RADIX");
    }

    if (radix > Character.MAX_RADIX) {
        throw new NumberFormatException("radix " + radix +
                                        " greater than Character.MAX_RADIX");
    }

    int result = 0;
    boolean negative = false;
    int i = 0, len = s.length();
    int limit = -Integer.MAX_VALUE;
    int multmin;
    int digit;

    if (len > 0) {
        char firstChar = s.charAt(0);
        if (firstChar < '0') { // Possible leading "+" or "-"
            if (firstChar == '-') {
                negative = true;
                limit = Integer.MIN_VALUE;
            } else if (firstChar != '+')
                throw NumberFormatException.forInputString(s);

            if (len == 1) // Cannot have lone "+" or "-"
                throw NumberFormatException.forInputString(s);
            i++;
        }
        multmin = limit / radix;
        while (i < len) {
            // Accumulating negatively avoids surprises near MAX_VALUE
            digit = Character.digit(s.charAt(i++),radix);
            if (digit < 0) {
                throw NumberFormatException.forInputString(s);
            }
            if (result < multmin) {
                throw NumberFormatException.forInputString(s);
            }
            result *= radix;
            if (result < limit + digit) {
                throw NumberFormatException.forInputString(s);
            }
            result -= digit;
        }
    } else {
        throw NumberFormatException.forInputString(s);
    }
    return negative ? result : -result;
}

这里有几个答案说尝试解析为整数并捕获 NumberFormatException 但你不应该这样做。

这种方式会创建异常对象并在每次调用它时生成一个堆栈跟踪,它不是一个整数。

Java 8 的更好方法是使用流:

boolean isInteger = returnValue.chars().allMatch(Character::isDigit);

对于科特林isDigitsOnly()也可用于Java的TextUtils.isDigitsOnly()String始终返回false它在前面的负号虽然性格剩下的只有是数字。 例如 -

/** For kotlin*/
var str = "-123" 
str.isDigitsOnly()  //Result will be false 

/** For Java */
String str = "-123"
TextUtils.isDigitsOnly(str) //Result will be also false 

所以我做了一个快速修复 -

 var isDigit=str.matches("-?\\d+(\\.\\d+)?".toRegex()) 
/** Result will be true for now*/

这仅适用于正整数。

public static boolean isInt(String str) {
    if (str != null && str.length() != 0) {
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) return false;
        }
    }
    return true;        
}

对于那些在提出问题多年后像我一样来到这里的读者,对于这个问题,我有一个更通用的解决方案。

/**
 * Checks, if the string represents a number.
 *
 * @param string the string
 * @return true, if the string is a number
 */
public static boolean isANumber(final String string) {
    if (string != null) {
        final int length = string.length();
        if (length != 0) {
            int i = 0;
            if (string.charAt(0) == '-') {
                if (length == 1) {
                    return false;
                }
                i++;
            }
            for (; i < length; i++) {
                final char c = string.charAt(i);
                if ((c <= PERIOD) || ((c >= COLON))) {
                    final String strC = Character.toString(c).toUpperCase();
                    final boolean isExponent = strC.equals("E");
                    final boolean isPeriod = (c == PERIOD);
                    final boolean isPlus = (c == PLUS);

                    if (!isExponent && !isPeriod && !isPlus) {
                        return false;
                    }
                }
            }
            return true;
        }
    }
    return false;
}
public class HelloWorld{

    static boolean validateIP(String s){
        String[] value = s.split("\\.");
        if(value.length!=4) return false;
        int[] v = new int[4];
        for(int i=0;i<4;i++){
            for(int j=0;j<value[i].length();j++){
                if(!Character.isDigit(value[i].charAt(j))) 
                 return false;
            }
            v[i]=Integer.parseInt(value[i]);
            if(!(v[i]>=0 && v[i]<=255)) return false;
        }
        return true;
    }

    public static void main(String[] argv){
        String test = "12.23.8.9j";
        if(validateIP(test)){
            System.out.println(""+test);
        }
    }
}
Integer.valueOf(string); 

大多数时候对我有用!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM