驼峰命名(CamelCase)与下划线命名(snake_case)互相转换

目录

一、使用Guava库

1.1 从驼峰转换到下划线

2..2 从下划线转换到驼峰

二、使用Apache Commons Lang库

2.1 从驼峰转换到下划线

2.1 从下划线转换到驼峰

三、使用Hutool库

3.1 从驼峰转换到下划线

3.1 从下划线转换到驼峰


一、使用Guava库

Guava库中的CaseFormat类提供了转换不同命名风格的方法。

pom.xml文件中添加如下依赖

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.1-jre</version>
</dependency>

1.1 从驼峰转换到下划线

import com.google.common.base.CaseFormat;
​
public class CaseConverter {
    public static void main(String[] args) {
        String camelCase = "camelCaseExample";
        String snakeCase = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, camelCase);
        System.out.println(snakeCase); // 输出: camel_case_example
    }
}

2..2 从下划线转换到驼峰

import com.google.common.base.CaseFormat;
​
public class CaseConverter {
    public static void main(String[] args) {
        String snakeCase = "camel_case_example";
        String camelCase = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, snakeCase);
        System.out.println(camelCase); // 输出: camelCaseExample
    }
}

二、使用Apache Commons Lang库

Apache Commons Lang库中的StringUtils类可以结合正则表达式使用来进行转换。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伏颜.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值