Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将表示数字i的字符串转换为整数_Java_String_Integer - Fatal编程技术网

Java 如何将表示数字i的字符串转换为整数

Java 如何将表示数字i的字符串转换为整数,java,string,integer,Java,String,Integer,我有一个字符串,我想转换成整数,我知道这可以通过使用Integer.parseInt(number) 或者使用Integer.parseInt(数字,基数)作为特定基数。 我可以使用上面的函数和if-else条件来检查由字符串表示的数字的基数 例如: 我想知道是否有任何内置函数可以将表示任意基数中的数字的字符串转换为相应的整数?正是为您的需要而设计的。从文档: 可选符号和/或基数说明符(“0x”、“0x”、“#”或前导零)后面的字符序列由Integer.parseInt方法以指定的基数(10、1

我有一个字符串,我想转换成整数,我知道这可以通过使用
Integer.parseInt(number)
或者使用
Integer.parseInt(数字,基数)
作为特定基数。
我可以使用上面的函数和if-else条件来检查由字符串表示的数字的基数
例如:

我想知道是否有任何内置函数可以将表示任意基数中的数字的字符串转换为相应的整数?

正是为您的需要而设计的。从文档:

可选符号和/或基数说明符(“0x”、“0x”、“#”或前导零)后面的字符序列由Integer.parseInt方法以指定的基数(10、16或8)进行分析

下面是负责基数区分的
Integer
源代码片段:

if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
    index += 2;
    radix = 16;
} else if (nm.startsWith("#", index)) {
    index ++;
    radix = 16;
} else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
    index ++;
    radix = 8;
}
if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
    index += 2;
    radix = 16;
} else if (nm.startsWith("#", index)) {
    index ++;
    radix = 16;
} else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
    index ++;
    radix = 8;
}