1. 程式人生 > >java 字串根據字元的ASCII碼值排序

java 字串根據字元的ASCII碼值排序

1.試卷答案和使用者答案進行對比時容易出現問題 ,需要按照當前字串進行打亂重排,然後進行比較


public static String ASCIISort(String str) {
    char[] test = new char[str.length()];
    StringBuilder sb = new StringBuilder();
    while (true) {
       String a = str;//直接讀取這行當中的字串。
       for (int i = 0; i < str.length(); i++) {
           //字串處理每次讀取一位。
           test[i] = a.charAt(i);
       }
       Arrays.sort(test);
       for (int i = 0; i < test.length; i++) {
           sb.append(test[i]);
    }
    String trim = sb.toString().trim();
    return trim;
}