将一句话去除符号后按大小排序

public static void main(String[] args) {
    String str = "Where are you from? I have an apple,but i want to home";
    // 大小写转换
    str = str.toLowerCase();
    String s = reveresStr(str);
    String s1 = sortStr(str);
    System.out.println(s);
    System.out.println(s1);
}

/***
 * @Description: 字符串翻转 
 * @param str
 * @return java.lang.String 
 * @throws
 * @author wdz
 * @date 2019/3/6 14:50 
 */
public static String reveresStr(String str){
    StringBuilder sb = new StringBuilder();
    // 根据非字母分割,剩余的全部是字母
    String[] split = str.split("[^a-zA-Z]");
    for (int i = 0; i < split.length; i++) {
        String s = split[i].toLowerCase();
        sb.append(s);
    }
    sb.reverse();
    return sb.toString();
}

/***
 * @Description: 按大小排序 冒泡排序
 * @param str
 * @return java.lang.String 
 * @throws
 * @author wdz
 * @date 2019/3/6 14:51 
 */
public static String sortStr(String str){
    char[] chars = str.toCharArray();
    for (int i = 0; i < chars.length-1; i++) {
        for (int j = 0; j < chars.length-i-1; j++) {
            // < > 控制大小排序
            if(chars[j] < chars[j+1]){
                char c = chars[j + 1];
                chars[j + 1] = chars[j];
                chars[j] = c;
            }
        }
    }
    StringBuilder s = new StringBuilder();
    for (char c :chars) {
        if(Character.isLetter(c)){
            s.append(c);
        }
    }
    return s.toString();
}
/***
 * @Description: 选择排序
 * @param str
 * @return java.lang.String 
 * @throws
 * @author wdz
 * @date 2019/3/6 15:09 
 */
private static String selectSort(String str) {
    char[] chars = str.toCharArray();
    int len = chars.length;
    char temp;
    int min;
    for (int i = 0; i < len-1; i++) {
        min = i;
        for (int j = i+1; j < len; j++) {
            // 控制大小排序
            if(chars[j] < chars[min]){
                min = j;
            }
        }
        temp = chars[i];
        chars[i] = chars[min];
        chars[min] = temp;
    }
    StringBuilder st = new StringBuilder();
    for (int i = 0; i < len; i++) {
        if(Character.isLetter(chars[i])){
            st.append(chars[i]);
        }
    }
    return st.toString();
}
/***
 * @Description: 插入排序
 * @param str
 * @return java.lang.String
 * @throws
 * @author wdz
 * @date 2019/3/6 15:21
 */
public static String insertSort(String str){
    char[] chars = str.toCharArray();
    char cur;
    int pre;
    int len = chars.length;
    for (int i = 1; i < len; i++) {
        cur = chars[i];
        pre = i - 1;
        while(pre >= 0 && chars[pre] < cur){
            chars[pre + 1] = chars[pre];
            pre--;
        }
        chars[pre + 1] = cur;
    }
    StringBuilder st = new StringBuilder();
    for (int i = 0; i < len; i++) {
        if(Character.isLetter(chars[i])){
            st.append(chars[i]);
        }
    }
    return st.toString();
}

 

/***
 * @Description: 使用Collections.sort排序
 * @param str
 * @return java.lang.String
 * @throws
 * @author wdz
 * @date 2019/3/6 15:50
 */
public static String collectionSort(String str){
    List<String> list = new ArrayList<>(str.length());
    char[] chars = str.toCharArray();
    for (int i = 0; i < chars.length; i++) {
        if(Character.isLetter(chars[i])){
            char s = chars[i];
            list.add(String.valueOf(s));
        }
    }
    // 从小到大排序
    // Collections.sort(list);
    Collections.sort(list, new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            // 参数调换位置可改变排序规则
            return o2.compareTo(o1);
        }
    });
    StringBuilder sb = new StringBuilder(list.size());
    for (String s : list) {
        sb.append(s);
    }
    return sb.toString();
}

结果
emohottnawitubelppanaevahimorfuoyeraerehw
ywwvuutttrrrppoooonnmmliihhhfeeeeeebaaaaa

更多请查看https://www.cnblogs.com/onepixel/articles/7674659.html,本文参考上述连接对字符串进行的排序,以上纯属个人理解,

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页