Java 驼峰命名法转下划线命名法

驼峰命名法转下划线命名法:

public class StringUtil {

    public static final char UNDERLINE='_';
    
    // 驼峰命名转下划线命名
    public static String camelToUnderline(String str) {
        if (str == null || str.trim().isEmpty()){  
            return "";  
        }

        int len = str.length();  
        StringBuilder sb = new StringBuilder(len);  
        for (int i = 0; i < len; i++) {  
            char c = str.charAt(i);  
            if (Character.isUpperCase(c)){  
                sb.append(UNDERLINE);  
                sb.append(Character.toLowerCase(c));  
            }else{  
                sb.append(c);  
            }  
        }  
        return sb.toString();
    }
    
    public static void main(String[] args) {
        System.out.println(camelToUnderline("myJavaClass"));
    }
}


你可能感兴趣的:(Java)