【发布时间】:2017-02-06 11:32:00
【问题描述】:
我写了一个提取最后六个字符的程序 这是我的代码
String numberString;
int number=123456;
// convert int to string
numberString = String.valueOf(number);
char[] chars = new char[7];
String napis = "S4P6W7M522SC3OXX55K3NN77666N34M2";
char[] array = napis.toCharArray();
// loop which pulls the last six characters
int index = 0;
for (int i = 0; i < array.length; i++) {
if (i >= 26 && i <= 32) {
chars[index] = array[i];
index++;
}
}
String Str = new String(napis);
System.out.print("String: " + napis);
// convert char to string
String kod = String.valueOf(chars);
System.out.print("Result " + kod);
// show new String
String newString = napis.replace(kod, numberString);
System.out.print("New String: " + newString);
//显示输出
Please write six number
123456
String: S4P6W7M522SC3OXX55K3NN77666N34M2
Result 6N34M2
New String: S4P6W7M522SC3OXX55K3NN77666N34M2
应该是
New String: S4P6W7M522SC3OXX55K3NN7766123456
我不知道为什么替换是不正确的
【问题讨论】:
-
为什么不简单地使用子字符串来删除最后 6 个字符?请定义
Character;) -
为什么你认为
numberString是123456?你能在替换之前输出numberString和number吗?