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

后端 未结 30 1441
野趣味
野趣味 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:19

    There is guava version:

    import com.google.common.primitives.Ints;
    
    Integer intValue = Ints.tryParse(stringValue);
    

    It will return null instead of throwing an exception if it fails to parse string.

提交回复
热议问题