日期:2014-05-20  浏览次数:20616 次

java将字符串中的数字重新排序,其他的非数字字符不变
比如字符串 "16a52Bc97",排序后为"12a56Bc79",该怎麽实现?
------最佳解决方案--------------------

public static void swap(int i,int j,char[] arr){
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void main(String[] args) throws Exception {
 String str = "16a52Bc97";//排序后为"12a56Bc79"
 char[] arr = str.toCharArray();
 for(int i=0;i<arr.length;i++){
 int preChar = arr[i];
 if( preChar < '0' 
------其他解决方案--------------------
 nextChar > '9'){
 continue;
 }
 if(preChar > nextChar){
 swap(i,j,arr);
 }
 }
 }
 System.out.println(new String(arr));
}

------其他解决方案--------------------
看不出来数字是按什么排序
------其他解决方案--------------------
相邻的数字分成单个字符,再排序……如“61”排序后是“16”
------其他解决方案--------------------
 preChar > '9'){
 continue;
 }
 for(int j=i+1;j<arr.length;j++){
 int nextChar = arr[j];
 if(nextChar < '0'