Java駝峰和下划線互相轉化


直接上代碼 :

package com.utils;


public class ChangeChar {

    public static final char UNDERLINE = '_';

    public static void main(String[] args) {

         /*駝峰轉下划線*/
        String str = "   itemName    bbbbCsss     ,  \n" +
                "    category     ,\n" +
                "\tbarCode\t \n" +
                "\tvolume\t  \n" +
                "\tlength\t  \n" +
                "\twidth\t \n" +
                "\theight\t \n" +
                "\tunitPrice\t \n" +
                "\tshelfLife\t \n" +
                "\tshelfLifeUnit\t  /\n" +
                "\t )";

        /*下划線轉駝峰*/
        String str2 = " ITEM_NAME         ,  \n" +
                "    CATEGORY     ,\n" +
                "\tBAR_CODE\t \n" +
                "\tVOLUME\t  \n" +
                "\tLENGTH\t  \n" +
                "\tWIDTH\t \n" +
                "\tHEIGHT\t \n" +
                "\tUNIT_PRICE\t \n" +
                "\tSHELF_LIFE\t \n" +
                "\tSHELF_LIFE_UNIT\t";

        /**
         * 測試
         * */
         /* charType=2 表示大寫, 其他情況都是小寫*/
        String STR_ABC = camelToUnderline(str, 2);   //  下划線大寫:ABC_DEF
        String str_abc = camelToUnderline(str, 1);   //  下划線小寫:abc_def
        System.out.println("駝峰轉化成下划線大寫 :" + STR_ABC);
        System.out.println("駝峰轉化成下划線小寫 :" + str_abc);

        String strAbc = underlineToCamel(str2);   //  下划線轉駝峰:abcDef
        System.out.println("下划線化成駝峰 :" + strAbc);

    }


    //駝峰轉下划線
    public static String camelToUnderline(String param, Integer charType) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append(UNDERLINE);
            }
            if (charType == 2) {
                sb.append(Character.toUpperCase(c));  //統一都轉大寫
            } else {
                sb.append(Character.toLowerCase(c));  //統一都轉小寫
            }


        }
        return sb.toString();
    }

    //下划線轉駝峰
    public static String underlineToCamel(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        Boolean flag = false; // "_" 后轉大寫標志,默認字符前面沒有"_"
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (c == UNDERLINE) {
                flag = true;
                continue;   //標志設置為true,跳過
            } else {
                if (flag == true) {
                    //表示當前字符前面是"_" ,當前字符轉大寫
                    sb.append(Character.toUpperCase(param.charAt(i)));
                    flag = false;  //重置標識
                } else {
                    sb.append(Character.toLowerCase(param.charAt(i)));
                }
            }
        }
        return sb.toString();
    }


}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM