Most efficient way of converting String to Integer in java

后端 未结 11 2028
面向向阳花
面向向阳花 2020-11-29 08:00

There are many ways of converting a String to an Integer object. Which is the most efficient among the below:

Integer.valueOf()
Integer.parseInt()
org.apache         


        
11条回答
  •  遥遥无期
    2020-11-29 08:37

    Another way is this method:

    public class stringtoInteger {
    
        private static int stringtoInteger(String x) {
            String value = "";
            for (int i = 0; i < x.length(); i++) {
                char character = x.charAt(i);
                if (Character.isDigit(character)) {
                    value = value + character;
                }
            }
            return Integer.parseInt(value);
        }
    }  
    

    Hope it helps!

提交回复
热议问题