比如:一个数组1ab43il526a写一个方法使其输出结果:1ab23il456 比如:一个数组1ab43il526a写一个方法使其输出结果:1ab23il456我的意思就是字母的数字不变,数字从小到大输出,求教各位大神怎么写一个方法实现~~~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 打错了,是字母的顺序不变,数字从小到大排列,怎么实现哦?这个String字符串只是举的个例子,可以写个传参的方法更好,谢谢给位大神了~~~~ String str="1ab43il526";public void mySort(String str){String[] s1;String[] s2;int k=0;for(int i=0;i<str.length();i++){s1[i]=str.subString(i,i+1);//将字符串放入字符数组里面if(s1[i].match("\\d")){s2[k++]=s1[i];//将s1中的数字提出来放入 s2}}Arrays.sort(s2);//排序for(int i=0;i<str.length();i++){if(s1[i].match("\\d")){s1[i]=s2[k++];//将排序后的数字插入原数组s1}}System.out.println(s1.toString);} for exampleString s = "1ab43il526a";String num = s.replaceAll("\\D+", ""); //把数字抽出来char[] nc = num.toCharArray();Arrays.sort(nc); //数字排序StringBuilder buf = new StringBuilder();char[] sc = s.toCharArray();for (int i=0, j=0; i<sc.length; i++) { //遍历原字符串 if (sc[i]>='0' && sc[i]<='9') { //如果某个位置是数字,就用排序后的数字替换 buf.append(nc[j++]); } else { buf.append(sc[i]); }}System.out.println(buf); 参考3楼的 重新修改了下 现在可以了public class Test1 { public static void main(String args[]){ String str="1ab43il526"; String[] s1 = new String[str.length()]; int kk = 0; for(int i=0;i<str.length();i++){ s1[i]=str.substring(i,i+1);//将字符串放入字符数组里面 if(s1[i].matches("\\d")){ kk++;//将s1中的数字提出来放入 s2 } } String[] s2 = new String[kk]; int k=0; for(int i=0;i<str.length();i++){ s1[i]=str.substring(i,i+1);//将字符串放入字符数组里面 if(s1[i].matches("\\d")){ s2[k++]=s1[i];//将s1中的数字提出来放入 s2 } } Arrays.sort(s2);//排序 int kkk = 0; for(int i=0;i<str.length();i++){ if(s1[i].matches("\\d")){ s1[i]=s2[kkk++];//将排序后的数字插入原数组s1 } } for(int i=0;i<str.length();i++){ System.out.print(s1[i]); } }} /** * @param args */ public static void main(String[] args) { String strNum = "1ab43il526a";// 要求输出1ab23il456 char[] a = strNum.toCharArray(); // 数字长度,计算数字个数 int numLength = 0; for (char c : a) { if (Character.isDigit(c)) { numLength++; } } // 数字数组 char[] numArray = new char[numLength]; // 记住数字的在字符数组中的下标 int[] subscript = new int[numLength]; int j = 0; for (int i = 0; i < a.length; i++) { if (Character.isDigit(a[i])) { numArray[j] = a[i]; subscript[j] = i; j++; System.out.println(a[i]); } } // 排序数字组数 java.util.Arrays.sort(numArray); for (int i = 0; i < numArray.length; i++) { // 根据原来的位置还原 a[subscript[i]] = numArray[i]; } System.out.println(new String(a)); // 输入出1ab23il456a } 优化一上面我8楼的代码 String strNum = "1ab43il526a";// 要求输出1ab23il456 char[] a = strNum.toCharArray(); String num = strNum.replaceAll("\\D+", ""); // 把数字抽出来 char[] numArray = num.toCharArray();// 数字数组 int[] subscript = new int[num.length()];// 记住数字的在字符数组中的下标 int j = 0; for (int i = 0; i < a.length; i++) { if (Character.isDigit(a[i])) { subscript[j] = i; j++; } } java.util.Arrays.sort(numArray);// 排序数字组数 for (int i = 0; i < numArray.length; i++) { // 根据原来的位置还原 a[subscript[i]] = numArray[i]; } System.out.println(new String(a)); // 输入出1ab23il456a 一道题目 半天没做出来 请教一逻辑测试! 关于局部变量的问题... 公司简单基础小问题,也考考大家^_^,每题目20分。 刚安装JDK,怎么在命令行里面没有javac,jar等命令? jexcel支持读写excel2003? 问两个问题 这段程序的正确输出是什么?先不要运行,思考一下!请各位赐教!并请解释一下原理。 新装了jbuilder9,把环境都搞坏了,请求帮忙 如何将.class文件转换为可执行文件? 一个小问题!!!!!!非常小!!!!!!! 为什么在Linux环境下的eclipse中swing代码移到myeclipse中生成的页面就乱码
这个String字符串只是举的个例子,可以写个传参的方法更好,谢谢给位大神了~~~~
String str="1ab43il526";
public void mySort(String str){
String[] s1;
String[] s2;
int k=0;
for(int i=0;i<str.length();i++){
s1[i]=str.subString(i,i+1);//将字符串放入字符数组里面
if(s1[i].match("\\d")){
s2[k++]=s1[i];//将s1中的数字提出来放入 s2
}
}
Arrays.sort(s2);//排序
for(int i=0;i<str.length();i++){
if(s1[i].match("\\d")){
s1[i]=s2[k++];//将排序后的数字插入原数组s1
}
}
System.out.println(s1.toString);
}
String s = "1ab43il526a";
String num = s.replaceAll("\\D+", ""); //把数字抽出来
char[] nc = num.toCharArray();
Arrays.sort(nc); //数字排序
StringBuilder buf = new StringBuilder();
char[] sc = s.toCharArray();
for (int i=0, j=0; i<sc.length; i++) { //遍历原字符串
if (sc[i]>='0' && sc[i]<='9') { //如果某个位置是数字,就用排序后的数字替换
buf.append(nc[j++]);
} else {
buf.append(sc[i]);
}
}
System.out.println(buf);
public class Test1 { public static void main(String args[]){
String str="1ab43il526";
String[] s1 = new String[str.length()];
int kk = 0;
for(int i=0;i<str.length();i++){
s1[i]=str.substring(i,i+1);//将字符串放入字符数组里面
if(s1[i].matches("\\d")){
kk++;//将s1中的数字提出来放入 s2
}
}
String[] s2 = new String[kk];
int k=0;
for(int i=0;i<str.length();i++){
s1[i]=str.substring(i,i+1);//将字符串放入字符数组里面
if(s1[i].matches("\\d")){
s2[k++]=s1[i];//将s1中的数字提出来放入 s2
}
}
Arrays.sort(s2);//排序
int kkk = 0;
for(int i=0;i<str.length();i++){
if(s1[i].matches("\\d")){
s1[i]=s2[kkk++];//将排序后的数字插入原数组s1
}
}
for(int i=0;i<str.length();i++){
System.out.print(s1[i]);
}
}
}
/**
* @param args
*/
public static void main(String[] args) { String strNum = "1ab43il526a";// 要求输出1ab23il456 char[] a = strNum.toCharArray();
// 数字长度,计算数字个数
int numLength = 0;
for (char c : a) {
if (Character.isDigit(c)) {
numLength++;
}
}
// 数字数组
char[] numArray = new char[numLength];
// 记住数字的在字符数组中的下标
int[] subscript = new int[numLength];
int j = 0;
for (int i = 0; i < a.length; i++) {
if (Character.isDigit(a[i])) {
numArray[j] = a[i];
subscript[j] = i;
j++;
System.out.println(a[i]);
}
} // 排序数字组数
java.util.Arrays.sort(numArray); for (int i = 0; i < numArray.length; i++) {
// 根据原来的位置还原
a[subscript[i]] = numArray[i]; } System.out.println(new String(a)); // 输入出1ab23il456a }
String strNum = "1ab43il526a";// 要求输出1ab23il456
char[] a = strNum.toCharArray();
String num = strNum.replaceAll("\\D+", ""); // 把数字抽出来
char[] numArray = num.toCharArray();// 数字数组
int[] subscript = new int[num.length()];// 记住数字的在字符数组中的下标
int j = 0;
for (int i = 0; i < a.length; i++) {
if (Character.isDigit(a[i])) {
subscript[j] = i;
j++;
}
}
java.util.Arrays.sort(numArray);// 排序数字组数
for (int i = 0; i < numArray.length; i++) {
// 根据原来的位置还原
a[subscript[i]] = numArray[i]; }
System.out.println(new String(a));
// 输入出1ab23il456a