public static int conversion(int n) {
String str = Integer.toString(n); //int to string
String str1= str.replace('0', '5'); //replace the character in string
int result1 = Integer.parseInt(str1); //string to int
int result = result1;
return result;
}我正在尝试将字符从'0‘替换为'5’。当前代码有效,但仅当前面没有'0‘时。
示例:'50005‘-> '55555’(o);'00005000‘-> '5555’(x) <-前端'0‘未更改
我应该添加什么或知道什么才能修复此错误?
发布于 2020-07-22 05:53:36
整数上的前导零在转换过程中被忽略。
String str = "0000123";
int n = Integer.valueOf(str);
System.out.println(str);
System.out.println(n);打印
0000123
123如果您在int前面加上一个0,它会将其视为八进制值,并忽略零。
int octalVal = 000031;
System.out.println(octalVal);
String decimalStringVal = Integer.toString(octalVal);
System.out.println(decimalStringVal);打印
25
25https://stackoverflow.com/questions/63023576
复制相似问题