문제

Is there a way to use RegEx in Java to replace all capital letters with an underscore and the same letter only lowercase?

Example: getSpecialString -> get_special_string

도움이 되었습니까?

해결책

Just try with:

"getSpecialString".replaceAll("([A-Z])", "_$1").toLowerCase();

다른 팁

There is a snakeCase method in underscore-java library:

import com.github.underscore.U;

public class Main {

    public static void main(String[] args) {
        U.snakeCase("getSpecialString"); // -> get_special_string
    }
}

This could also work using Regex!

"MyCamelCase".replaceAll(/(?<!^)[A-Z]/g, (match) => `_${match}`).toLowerCase();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top