【发布时间】:2021-08-27 16:49:40
【问题描述】:
如何将字符串中的子字符串的 ASCII 值转换为其字符?例如:
如果我输入为
H105 68u100e33 65r101 89o117?
我需要输出为
Hi Dude! How Are You?
在上面的输入中,每个字母或空格后面都有一个 ASCII 值。
char[] a = new char[2];
char t;
String temp,output="";
for(int i=0;i<str.length()-1;i++){
a[0]=str.charAt(i);
a[1]=str.charAt(i+1);
if(Character.isDigit(a[0])){
if(Character.isDigit(a[1])){
String con=String.valueOf(a[0])+String.valueOf(a[1]);
int n=Integer.parseInt(con);
output=output+(char)n;
}else{
//nothing
}
}else{
output=output+a[0];
}
}
System.out.println("output : "+output);
我尝试过类似的方法,但它在 3 位 ASCII 值时失败,而且有时由于charAt(i+1) 语句,我会遇到数组索引出站错误。
如何将 ASCII 值更改为它的字符并形成一个句子?
【问题讨论】:
-
你的最后一句话总是包含字母吗?
标签: java