アルパカの命名変数を複数の単語からなる変数名に簡単に変換する方法

1335 ワード

アルパカの名前を複数の単語からなる変数名に変換します.たとえば、helloWorldをhello_に変換します.world
public class Client {
    public static String toHump(String str) {
        String rs = "";

        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (Character.isUpperCase(c)) {
                rs += "_" + Character.toLowerCase(c);
            } else {
                rs += c;
            }
        }
        return rs;
    }

    public static void main(String[] args) {
        String str = "helloWorld"; // hello_world
        System.out.println(toHump(str));
    }
}