JAVA 字符串处理

来源:百度知道 编辑:UC知道 时间:2024/05/10 21:51:23
一道上机题不知道怎么做,题目是给一个字符串"xy5dew_dp6n",如果是数字的话,后面的字符循环数字次,如果是"_",用"@"替换,最终结果是"xy5dddddew@dp6nnnnnn"其他不变,怎么求解?
能给出相应的JAVA代码吗?

String t = "xy5dew_dp6n";
int n = 0;
int m = 0;

StringBuffer txt=new StringBuffer();
int strLen = t.length();
for(int i = 0; i < strLen; i++){
if(t.charAt(i) == '_')
txt.append('@');
else if(t.charAt(i) >= 48 && t.charAt(i) <= 57){
n = t.charAt(i) - 48;
System.out.println(n);
if(i < strLen - 1){
for(m = 0; m < n; m++){
txt.append(t.charAt(i + 1));
}
i++;
}
}
else
txt.append(t.charAt(i));
}

t = new String(txt);
System.out.println(t);

xy55dew_dp6n?

将字符串一个一个字符进行提取,当取出来的是数字时就再取一个字符进行循环,如果是_就替换成@,其余字符不变

Sring str = "xy5dew_dp6n";
str = srt.replace.("_","@");
/*
后面用正则表达式处理,不然你不好处理连在一起的数字。
代码很多,我就不写了。
*/

遍的不是很好,参考一下巴。如最