用java实现把字符串中的所有d大写字母转换成数字,其他的不变

2024-11-30 08:46:06
推荐回答(4个)
回答1:


public class Change {//是这样吗
public static void main(String[] args) {
String inStr = "AB12D3Za#45";//qqgroup21/77/7/12
StringBuffer outStr = new StringBuffer();
for (int i = 0; i < inStr.length(); i++) {
int c = (int)inStr.charAt(i);
if(65<=c&&c<=91){
outStr.append(c-64);
}else{
outStr.append(inStr.charAt(i));
}
}

System.out.println(outStr);
}
}

回答2:

编码与字符的联系!例如A的UTF编码是65,B的编码是66,减去55得到10,11

回答3:

    a = "AB12D3Za#45";
     a = a.replace("A","1");
     a = a.replace("B","2");
     ....
     ...
     ....
     a = a.replace("Z","26");

回答4:

public class ReplaceTest {
private static HashMap map = new HashMap();
static {
map.put(Character.valueOf('A'), "1");
map.put(Character.valueOf('B'), "2");
map.put(Character.valueOf('C'), "3");
map.put(Character.valueOf('D'), "4");
map.put(Character.valueOf('E'), "5");
map.put(Character.valueOf('F'), "6");
map.put(Character.valueOf('G'), "7");
map.put(Character.valueOf('H'), "8");
map.put(Character.valueOf('I'), "9");
map.put(Character.valueOf('G'), "10");
map.put(Character.valueOf('K'), "11");
map.put(Character.valueOf('L'), "12");
map.put(Character.valueOf('M'), "13");
map.put(Character.valueOf('N'), "14");
map.put(Character.valueOf('O'), "15");
}

public static String replace(String string) {
char[] cs = string.toCharArray();
StringBuffer sb = new StringBuffer();
int count = 0;
for (int i = 0; i < cs.length; i++) {
count=0;
for (Iterator iterator = map.keySet().iterator(); iterator
.hasNext();) {
Character name = (Character) iterator.next();
System.out.println(name + "and" + cs[i]);
if (name.equals(cs[i])) {
sb.append(map.get(name));
break;
}else {
count++;
}
if (count == map.size()) {
sb.append(cs[i]);
count=0;
}
}
}

return sb.toString();
}

public static void main(String[] args) {
System.out.println(replace("AB12D3Za#45"));

}
}
map.put()后面自己去加,虽然比较麻烦