在Java中用下划线+小写字母替换大写字母?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23662527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replace capital letter with underscore + lowercase letter in Java?
提问by dragostis
Is there a way to use RegEx in Java to replace all capital letters with an underscore and the same letter only lowercase?
有没有办法在 Java 中使用 RegEx 用下划线替换所有大写字母,而相同的字母只用小写?
Example:
getSpecialString
-> get_special_string
示例:
getSpecialString
->get_special_string
采纳答案by hsz
Just try with:
只需尝试:
"getSpecialString".replaceAll("([A-Z])", "_").toLowerCase();
回答by Valentyn Kolesnikov
There is a snakeCase method in underscore-javalibrary:
underscore-java库中有一个 snakeCase 方法:
import com.github.underscore.lodash.U;
public class Main {
public static void main(String[] args) {
U.snakeCase("getSpecialString"); // -> get_special_string
}
}