java 骆驼model转换,用于将CamelCase转换为java中的camel_case的正则表达式

本文探讨如何使用更简洁的方法将CamelCase转换为LOWER_UNDERSCORE格式,避免了繁琐的替换操作。作者分享了一种利用内置函数或库的解决方案,如CaseFormat类的实例化,以实现快速、易读的字符串格式转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I understand why the desired output is not given for converting using regex a string like FooBar to Foo_Bar which instead gives Foo_Bar_. I could have done something with String.substring substring(0, string.length() - 2) or just replaced the last character, but I think there is a better solution to such a scenario.

Here is the code:

String regex = "([A-Z][a-z]+)";

String replacement = "$1_";

"CamelCaseToSomethingElse".replaceAll(regex, replacement);

/*

outputs: Camel_Case_To_Something_Else_

desired output: Camel_Case_To_Something_Else

*/

Question: Looking for a neater way to get the desired output?

解决方案

in your case, something like:

CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "SomeInput");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值