比如:一个数组1ab43il526a写一个方法使其输出结果:1ab23il456
我的意思就是字母的数字不变,数字从小到大输出,求教各位大神怎么写一个方法实现~~~

解决方案 »

  1.   

    打错了,是字母的顺序不变,数字从小到大排列,怎么实现哦?
    这个String字符串只是举的个例子,可以写个传参的方法更好,谢谢给位大神了~~~~
      

  2.   


    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);
    }
      

  3.   

    for example
    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);
      

  4.   

    参考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]);
    }

    }
    }
      

  5.   


    /**
     * @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 }
      

  6.   

    优化一上面我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