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

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

    If your String array contains pure Integers and Strings, code below should work. You only have to look at first character. e.g. ["4","44","abc","77","bond"]

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

提交回复
热议问题