Replace uppercase letter with underscore + lowercase character in Java?

Is there a way to use RegEx in Java to replace all uppercase letters with an underscore and a single letter only in lower case?

Example: getSpecialStringget_special_string

+4
source share
2 answers

Just try:

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

There is a snakeCase method in underscore-java :

import com.github.underscore.lodash.$;

public class Main {

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

Source: https://habr.com/ru/post/1540572/


All Articles