String myString = "1234";
int foo = Integer.parseInt(myString);
If you look at the Java Documentation you'll notice the "catch" is that this function can throw a NumberFormatException
, which of course you have to handle:
int foo;
try {
foo = Integer.parseInt(myString);
}
catch (NumberFormatException e)
{
foo = 0;
}
(This treatment defaults a malformed number to 0
, but you can do something else if you like.)
Alternatively, you can use an Ints
method from the Guava library, which in combination with Java 8's Optional
, makes for a powerful and concise way to convert a string into an int:
import com.google.common.primitives.Ints;
int foo = Optional.ofNullable(myString)
.map(Ints::tryParse)
.orElse(0)
2016年5月5日 ... 一、int转String有三种方式(1)num + ""(2)String.valueOf(num)(3)Integer.toString( num)//int => Stringint num = 123456789;//(1)num + ""long start ...
We can convert String to an int in java using Integer.parseInt() method. To convert String into Integer, we can use Integer.valueOf() method which returns ...
2、 int i = Integer.valueOf(my_str).intValue();. 注: 字串转成Double, Float, Long 的 方法大同小异. 2 如何将整数int 转换成字串String ?
2008年11月21日 ... String转换为int型//convert str(String) to i(int)String str;int i = Integer.parseInt(str);int 型转换为String//conver i(int) to str(String)int i;String str = i.
String mystr = mystr.replaceAll("[^\\d]", ""); int number = Integer.parseInt(mystr);.
2016年3月14日 ... 通过toString()方法,可以把整数(包括0)转化为字符串,但是Integer如果是null 的话,就会报空指针异常。 2、String.valueOf(Object obj)可以把整 ...
2018年3月28日 ... 在Java 中要将String 类型转化为int 类型时,需要使用Integer 类中的 parseInt() 方法 或者 valueOf() 方法进行转换.例1:123456String str = "123"; ...
2018年5月28日 ... 一般在Java中String转为Int主要有两种方法: Integer.parseInt(str); Integer.valueOf (str); ps:两者的不同之处: Integer.parseInt(s)返回值为Int ...
Nov 23, 2020 ... 1. Use Integer.parseInt() to Convert a String to an Integer. This method returns the string as a primitive type int. If the string does not ...
1. Java – Convert String to int using Integer.parseInt(String) ... Here we have a String str with the value “1234”, the method parseInt() takes str as argument and ...
2018年10月17日 ... String str = "123"; int a = Integer.parseInt(str); System.out.println(a); Integer b = Integer.valueOf(str); System.out.println(b); int c = Integer.
2018年9月4日 ... 1 Integer.parseInt(String str)方法2 Integer.parseInt(String s, int radix)方法3 Character....
May 17, 2017 ... Java String to int conversion. Overlooking the exception it can throw, use this: int i = Integer.parseInt(myString);. If the String signified by the ...
Converting String to Integer. For converting String to Integer or int, there are four built-in approaches. The wrapper class Integer provides a few methods ...
May 1, 2021 ... toString(int) or String.valueOf(int). But if your variable is already an instance of Integer (wrapper class of the primitive type int), it is better to just ...
1. The toString() method. This method is present in many Java classes. It returns a string. · 2. String.valueOf(). Pass your integer (as an int or Integer) to this method ...
Feb 27, 2021 ... The most direct solution to convert a string to an integer in Java is to use the parseInt method of the Integer class. parseInt converts the String to ...
Read in next the characters until the next non-digit charcter or the end of the input is reached. The rest of the string is ignored. Convert these digits into an integer ...