c语言中将整数转换成字符串_如何在Java中将字符串转换为Int(整数)?

c语言中将整数转换成字符串

c语言中将整数转换成字符串

Java programming language provides different variable types. One of the most used types is String and Int or integer. During the usage of these variable types, we may need to convert them. In this tutorial, we will examine string to int and int to string variables types conversion in Java programming language.

Java编程语言提供了不同的变量类型。 字符串和整数或整数是最常用的类型之一。 在使用这些变量类型期间,我们可能需要转换它们。 在本教程中,我们将研究Java编程语言中从字符串到int以及从int到字符串变量类型的转换。

用parseInt()函数转换 (Convert with parseInt() Function)

parseInt() is a function that can be used to convert string to int. parseInt() is provided by Integer class which is mainly created to parse and convert different variable types into Integer. In this example, we will create a string type variable named age and then convert to the integer.

parseInt()是可用于将字符串转换为int的函数。 parseInt()Integer类提供,该类主要用于分析并将不同的变量类型转换为Integer。 在此示例中,我们将创建一个名为age的字符串类型变量,然后将其转换为整数。

String age="20";

int myage= Integer.parseInt(age);

System.out.println(myage);

parseInt() will return a primitive int value which is not an object.

parseInt()将返回不是对象的原始int值。

用valueOf()函数转换 (Convert with valueOf() Function)

There are an alternative way and function to convert string to int which is named valueOf(). valueOf() function is provided by Integer class and can be used as parseInt() function. In this example, we will create a string type variable named age and then convert to the integer.

还有一种将字符串转换为int的方法和函数,称为valueOf()valueOf()函数由Integer类提供,可以用作parseInt()函数。 在此示例中,我们将创建一个名为age的字符串类型变量,然后将其转换为整数。

String age="20";

int myage= Integer.valueOf(age);

System.out.println(myage);

valueOf() function will return Integer Object which is more complex then primitive integer value.

valueOf()函数将返回比原始整数值复杂的Integer Object。

用decode()函数转换 (Convert with decode() Function)

Intger class also provides the decode() function. decode() function can be used like other functions where we will provide the string variable to the static decode() function.

Intger类还提供了Intger decode()函数。 可以像其他函数一样使用decode()函数,在该函数中,我们将字符串变量提供给静态decode()函数。

String age="20";

int myage= Integer.decode(age);

System.out.println(myage);

用toInt()函数转换 (Convert with toInt() Function)

Another alternative for String to Int conversion is toInt() function .toInt() function is provided by NumberUtils class. We will just provide the string variable or value like below.

将String转换为Int的另一种方法是toInt()函数。 toInt()函数由NumberUtils类提供。 我们将只提供如下所示的字符串变量或值。

String age="20";

int myage= NumberUtils.toInt(age);

System.out.println(myage);

捕获转换异常 (Catch Convert Exceptions)

During the convert operation of string to int, we may get exceptions about conversion. If provided string variable contains non-numeric values like a letter, punctuation, etc. the given functions parseInt() and valueOf() will throw an exception about the convert operation. In this example, we will get NumberFormatException because given string provides letter like B.

在将字符串转换为int的转换过程中,我们可能会获得有关转换的异常。 如果提供的字符串变量包含非数字值,例如字母,标点符号等,则给定的函数parseInt()和valueOf()将引发有关转换操作的异常。 在此示例中,我们将获得NumberFormatException因为给定的字符串提供了类似于B字母。

String age="20B";

try{

   int myage= Integer.valueOf(age);
   System.out.println(myage);

}catch (NumberFormatException e){

   throw e;

}

有效和无效的转换 (Valid and Invalid Conversions)

There are different possibilities during string to int convert. Here some of them are valid and convert to int and some of the invalid which will not convert into int and throw an exception. We will use parseInt() function but given string values will throw the same error for other string to int conversion functions like decode() , valueOf() , parseInt() .

字符串到int转换期间有不同的可能性。 在这里,其中一些是有效的并转换为int,而一些无效的则不会转换为int并引发异常。 我们将使用parseInt()函数,但是给定的字符串值将对其他字符串向int转换函数(如decode()valueOf()parseInt()抛出相同的错误。

Integer.parseInt("1"); //OK

Integer.parseInt("-1"); //OK

Integer.parseInt("+1"); //OK

Integer.parseInt(" 1"); //Exception because of space

Integer.parseInt("2147483650"); //Exception integer overflow because an integer can cold up to 2,147,483,647

Integer.parseInt("1.5"); //Exception because  not whole number

Integer.parseInt(""); //Exception no value to convert
LEARN MORE  strlen() Function In C and C++ Tutorial with Examples
在C和C ++教程中了解更多strlen()函数的示例

翻译自: https://www.poftut.com/how-to-convert-string-to-int-integer-in-java/

c语言中将整数转换成字符串

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值