字符转换的问题,高手们都进来吧! 本帖最后由 Papaver_Flower 于 2012-02-16 18:51:15 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1、难吗?2、按题目的要求,你的答案应该是@hjkiiio@jie_nhhhf4 String x = "_hjk2io_jie@n3hf4_"; if (x.charAt(x.length() - 1) == '_') x = x.substring(0, x.length() - 1); char[] temp = x.toCharArray(); StringBuffer y = new StringBuffer(); for (int i = 0; i < x.length(); i++) { if (temp[i] == '_') y.append('@'); else if (temp[i] == '@') y.append('_'); else if (temp[i] >= '0' && temp[i] <= '9' && i < x.length() - 1 && ('0' < temp[i + 1] || temp[i + 1] < '9')) y.append(temp[i + 1]).append(temp[i + 1]); else y.append(temp[i]); } System.out.println(y); toCharArray(),转化成char类型数组,然后全数组搜索用for循环,然后找到你要替换的就OK啦 public class Test{ public static String change(String str) { StringBuilder strBuilder = new StringBuilder(str); if (strBuilder.charAt(strBuilder.length() - 1) == '_') { strBuilder.setLength(strBuilder.length() - 1); } for (int i = 0; i < strBuilder.length(); i++) { if (strBuilder.charAt(i) == '_') { strBuilder.setCharAt(i, '@'); } else if (strBuilder.charAt(i) == '@') { strBuilder.setCharAt(i, '_'); } else if (Character.isDigit(strBuilder.charAt(i)) && i != strBuilder.length() - 1) { int t = Integer.parseInt(strBuilder.substring(i, i + 1)); strBuilder.deleteCharAt(i); for (int j = 1; j < t; j++) { strBuilder.insert(i, strBuilder.charAt(i)); } } } return strBuilder.toString(); } public static void main(String[] args) { String str = "_hjk2io_jie@n3hf4_"; System.out.println(change(str)); }} 根据题意 修正一下,当然能用上正则能清晰很多,但是我看见正则就头晕 String x = "_hjk2io_jie@n3hf4_"; if (x.charAt(x.length() - 1) == '_') x = x.substring(0, x.length() - 1); char[] temp = x.toCharArray(); StringBuffer y = new StringBuffer(); for (int i = 0; i < x.length(); i++) { if (temp[i] == '_') y.append('@'); else if (temp[i] == '@') y.append('_'); else if ((temp[i] >= '0' && temp[i] <= '9') && i < x.length() - 1 && (('a' < temp[i + 1] || temp[i + 1] < 'z') && ('A' < temp[i + 1] || temp[i + 1] < 'Z'))) y.append(temp[i + 1]).append(temp[i + 1]); else y.append(temp[i]); } System.out.println(y); 修正一下,当然用上正则能清晰很多,但是我看见正则就头晕! String x = "_hjk2io_jie@n3hf4_"; if (x.charAt(x.length() - 1) == '_') x = x.substring(0, x.length() - 1); char[] temp = x.toCharArray(); StringBuffer y = new StringBuffer(); for (int i = 0; i < x.length(); i++) { if (temp[i] == '_') y.append('@'); else if (temp[i] == '@') y.append('_'); else if ((temp[i] >= '0' && temp[i] <= '9') && i < x.length() - 1 && (('a' < temp[i + 1] || temp[i + 1] < 'z') && ('A' < temp[i + 1] || temp[i + 1] < 'Z'))) y.append(temp[i + 1]).append(temp[i + 1]); else y.append(temp[i]); } System.out.println(y); //笨方法,不知正则能不能,待研究String x ="_hjk2io_jie@n3hf4_"; System.out.println(x); String x1 = x.replace("_","@"); System.out.println(x1); String x2 =""; if((char)x1.charAt(x1.length()-1)=='@') { for(int i=0;i<x1.length()-1;i++) { x2=x2+x1.charAt(i); } } else { x2=x1; } System.out.println(x2); String x3 =""; int a = 0; for(int i=0;i<x2.length();i++) { if(a>0) { for(int j=0;j<a;j++) { x3=x3+x2.charAt(i); } } a = x2.charAt(i); if(a>=48 && a <=57) { a=a-48; } else { a=0; x3=x3+x2.charAt(i); } } System.out.println(x3); 关于内部类的问题。求助。。 java for循环的另一种形式 Swing控件上不显示刷新时间 已经使用了Thread...很困扰... Integer[] 转 String[] 数组 the newer:swt/jface(译自swt in action) 有谁知道怎么样在行为事件中得到包含触发事件的元件的类的实例 ? 关于JdbComboBox的用法 修改Look And Feel 后如何使JFrame和ContentPanel面板上的控件更新 如何得到系统的默认字符集 我不想活了!!!!!!!!!! Java,SWT,如何增加选项卡按钮的宽度? java怎么学啊
2、按题目的要求,你的答案应该是@hjkiiio@jie_nhhhf4
String x = "_hjk2io_jie@n3hf4_";
if (x.charAt(x.length() - 1) == '_')
x = x.substring(0, x.length() - 1);
char[] temp = x.toCharArray();
StringBuffer y = new StringBuffer(); for (int i = 0; i < x.length(); i++) {
if (temp[i] == '_')
y.append('@');
else if (temp[i] == '@')
y.append('_');
else if (temp[i] >= '0' && temp[i] <= '9' && i < x.length() - 1
&& ('0' < temp[i + 1] || temp[i + 1] < '9'))
y.append(temp[i + 1]).append(temp[i + 1]);
else
y.append(temp[i]);
}
System.out.println(y);
public class Test
{
public static String change(String str)
{
StringBuilder strBuilder = new StringBuilder(str);
if (strBuilder.charAt(strBuilder.length() - 1) == '_')
{
strBuilder.setLength(strBuilder.length() - 1);
}
for (int i = 0; i < strBuilder.length(); i++)
{
if (strBuilder.charAt(i) == '_')
{
strBuilder.setCharAt(i, '@');
}
else if (strBuilder.charAt(i) == '@')
{
strBuilder.setCharAt(i, '_');
}
else if (Character.isDigit(strBuilder.charAt(i)) && i != strBuilder.length() - 1)
{
int t = Integer.parseInt(strBuilder.substring(i, i + 1));
strBuilder.deleteCharAt(i);
for (int j = 1; j < t; j++)
{
strBuilder.insert(i, strBuilder.charAt(i));
}
}
}
return strBuilder.toString();
}
public static void main(String[] args)
{
String str = "_hjk2io_jie@n3hf4_";
System.out.println(change(str));
}
}
if (x.charAt(x.length() - 1) == '_')
x = x.substring(0, x.length() - 1);
char[] temp = x.toCharArray();
StringBuffer y = new StringBuffer(); for (int i = 0; i < x.length(); i++) {
if (temp[i] == '_')
y.append('@');
else if (temp[i] == '@')
y.append('_');
else if ((temp[i] >= '0' && temp[i] <= '9')
&& i < x.length() - 1
&& (('a' < temp[i + 1] || temp[i + 1] < 'z') && ('A' < temp[i + 1] || temp[i + 1] < 'Z')))
y.append(temp[i + 1]).append(temp[i + 1]);
else
y.append(temp[i]);
}
System.out.println(y);
if (x.charAt(x.length() - 1) == '_')
x = x.substring(0, x.length() - 1);
char[] temp = x.toCharArray();
StringBuffer y = new StringBuffer(); for (int i = 0; i < x.length(); i++) {
if (temp[i] == '_')
y.append('@');
else if (temp[i] == '@')
y.append('_');
else if ((temp[i] >= '0' && temp[i] <= '9')
&& i < x.length() - 1
&& (('a' < temp[i + 1] || temp[i + 1] < 'z') && ('A' < temp[i + 1] || temp[i + 1] < 'Z')))
y.append(temp[i + 1]).append(temp[i + 1]);
else
y.append(temp[i]);
}
System.out.println(y);
//笨方法,不知正则能不能,待研究
String x ="_hjk2io_jie@n3hf4_";
System.out.println(x);
String x1 = x.replace("_","@");
System.out.println(x1);
String x2 ="";
if((char)x1.charAt(x1.length()-1)=='@')
{
for(int i=0;i<x1.length()-1;i++)
{
x2=x2+x1.charAt(i);
}
}
else
{
x2=x1;
}
System.out.println(x2);
String x3 ="";
int a = 0;
for(int i=0;i<x2.length();i++)
{
if(a>0)
{
for(int j=0;j<a;j++)
{
x3=x3+x2.charAt(i);
}
}
a = x2.charAt(i);
if(a>=48 && a <=57)
{
a=a-48;
}
else
{
a=0;
x3=x3+x2.charAt(i);
}
}
System.out.println(x3);