Java實(shí)現(xiàn)駝峰、下劃線互轉(zhuǎn)的方法
Java實(shí)現(xiàn)駝峰、下劃線互轉(zhuǎn)
1.使用 Guava 實(shí)現(xiàn)
先引入相關(guān)依賴
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
```1
1.1 駝峰轉(zhuǎn)下劃線
```java
public static void main(String[] args) {
String resultStr = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "userName");
System.out.println("轉(zhuǎn)換后結(jié)果是:"+resultStr);
}
轉(zhuǎn)換后結(jié)果是:user_name1.2 下劃線轉(zhuǎn)駝峰
public static void main(String[] args) {
String resultStr = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "user_name");
System.out.println("轉(zhuǎn)換后結(jié)果是:"+resultStr);
}
轉(zhuǎn)換后結(jié)果是:userName2.自定義代碼轉(zhuǎn)
2.1駝峰轉(zhuǎn)下劃線
private static final Pattern TPATTERN = Pattern.compile("[A-Z0-9]");
private String teseDemo(String str) {
Matcher matcher = TPATTERN.matcher(str);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
}
matcher.appendTail(sb);
return sb.toString();
}2.2下劃線轉(zhuǎn)駝峰
private static final char UNICON = '_';
private String underlineToCamel(String param) {
if (StringUtils.isBlank(param)) {
return "";
}
int len = param.length();
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; i++) {
char c = Character.toLowerCase(param.charAt(i));
if (c == UNICON) {
if (++i < len) {
sb.append(Character.toUpperCase(param.charAt(i)));
}
} else {
sb.append(c);
}
}
return sb.toString();
}到此這篇關(guān)于Java實(shí)現(xiàn)駝峰、下劃線互轉(zhuǎn)的示例代碼的文章就介紹到這了,更多相關(guān)java駝峰、下劃線內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中的StringUtils.isBlank()方法解讀
這篇文章主要介紹了Java中的StringUtils.isBlank()方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-05-05
Spring?Security內(nèi)置過濾器的維護(hù)方法
這篇文章主要介紹了Spring?Security的內(nèi)置過濾器是如何維護(hù)的,本文給我們分析一下HttpSecurity維護(hù)過濾器的幾個(gè)方法,需要的朋友可以參考下2022-02-02
SpringBoot自定義注解使用讀寫分離Mysql數(shù)據(jù)庫的實(shí)例教程
這篇文章主要給大家介紹了關(guān)于SpringBoot自定義注解使用讀寫分離Mysql數(shù)據(jù)庫的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
淺談Java HttpURLConnection請(qǐng)求方式
這篇文章主要介紹了淺談Java HttpURLConnection請(qǐng)求方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Spring學(xué)習(xí)之動(dòng)態(tài)代理(JDK動(dòng)態(tài)代理和CGLIB動(dòng)態(tài)代理)
本篇文章主要介紹了Spring學(xué)習(xí)之動(dòng)態(tài)代理(JDK動(dòng)態(tài)代理和CGLIB動(dòng)態(tài)代理),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07

