Java實現(xiàn)駝峰和下劃線互相轉(zhuǎn)換的示例代碼
前言
基本語法
首先我們要知道java的基礎語法。
1.由26個英文字母大小寫,0-9,_或$組成
2.數(shù)字不可以開頭
3.不可以使用關鍵字和保留字,但是能包括關鍵字和保留字
4.Java中嚴格區(qū)分大小寫,長度無限制
5.標識符不能包括空格
6.取名盡量做到“見名知意”
駝峰命名法
駱駝式命名法(Camel-Case)又稱駝峰式命名法,是電腦程式編寫時的一套命名規(guī)則(慣例)。
正如它的名稱CamelCase所表示的那樣,是指混合使用大小寫字母來構成變量和函數(shù)的名字。
程序員們?yōu)榱俗约旱拇a能更容易的在同行之間交流,所以多采取統(tǒng)一的可讀性比較好的命名方式。
例如,下面是分別用駱駝式命名法和下劃線法命名的同一個函數(shù):
printEmployeePaychecks();
print_employee_paychecks();
第一個函數(shù)名使用了駝峰命名法——函數(shù)名中的每一個邏輯斷點都有一個大寫字母來標記;
第二個函數(shù)名使用了下劃線法----函數(shù)名中的每一個邏輯斷點都有一個下劃線來標記。
1.駝峰與下劃線互轉(zhuǎn)
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 駝峰法-下劃線互轉(zhuǎn)
* @author cshaper
* @since 2015.07.04
* @version 1.0.0
*/
public class UnderlineToCamelUtils {
/**
* 下劃線轉(zhuǎn)駝峰法
* @param line 源字符串
* @param smallCamel 大小駝峰,是否為小駝峰
* @return 轉(zhuǎn)換后的字符串
*/
public static String underlineToCamel(String line,boolean smallCamel){
if(line==null||"".equals(line)){
return "";
}
StringBuffer sb=new StringBuffer();
Pattern pattern=Pattern.compile("([A-Za-z\\d]+)(_)?");
Matcher matcher=pattern.matcher(line);
while(matcher.find()){
String word=matcher.group();
sb.append(smallCamel&&matcher.start()==0?Character.toLowerCase(word.charAt(0)):Character.toUpperCase(word.charAt(0)));
int index=word.lastIndexOf('_');
if(index>0){
sb.append(word.substring(1, index).toLowerCase());
}else{
sb.append(word.substring(1).toLowerCase());
}
}
return sb.toString();
}
/**
* 駝峰法轉(zhuǎn)下劃線
* @param line 源字符串
* @return 轉(zhuǎn)換后的字符串
*/
public static String camelToUnderline(String line){
if(line==null||"".equals(line)){
return "";
}
line=String.valueOf(line.charAt(0)).toUpperCase().concat(line.substring(1));
StringBuffer sb=new StringBuffer();
Pattern pattern=Pattern.compile("[A-Z]([a-z\\d]+)?");
Matcher matcher=pattern.matcher(line);
while(matcher.find()){
String word=matcher.group();
sb.append(word.toUpperCase());
sb.append(matcher.end()==line.length()?"":"_");
}
return sb.toString();
}
}2.測試
public static void main(String[] args) {
String line="I_HAVE_AN_IPANG3_PIG";
String camel=underlineToCamel(line,true);
System.out.println(camel);
System.out.println(camelToUnderline(camel));
}
3.方法補充
除了上面的方法,本文還為大家準備了更簡短的方法,需要的可以參考一下
/***
* <p>
* 將駝峰轉(zhuǎn)為下劃線
* </p >
* @param str
* @return java.lang.String
* @author comapss
* @date 2022/5/9 0:01
* @since 1.0.0
**/
public static String humpToUnderline(String str) {
Pattern compile = Pattern.compile("[A-Z]");
Matcher matcher = compile.matcher(str);
StringBuffer sb = new StringBuffer();
while(matcher.find()) {
matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
}
matcher.appendTail(sb);
return sb.toString();
}
/***
* <p>
* 將下劃線轉(zhuǎn)為駝峰
* </p >
* @param str
* @return java.lang.String
* @author comapss
* @date 2022/5/9 0:01
* @since 1.0.0
**/
public static String underlineToHump(String str) {
str = str.toLowerCase();
Pattern compile = Pattern.compile("_[a-z]");
Matcher matcher = compile.matcher(str);
StringBuffer sb = new StringBuffer();
while(matcher.find()) {
matcher.appendReplacement(sb, matcher.group(0).toUpperCase().replace("_",""));
}
matcher.appendTail(sb);
return sb.toString();
}
以上就是Java實現(xiàn)駝峰和下劃線互相轉(zhuǎn)換的示例代碼的詳細內(nèi)容,更多關于Java駝峰 下劃線互轉(zhuǎn)的資料請關注腳本之家其它相關文章!
相關文章
SSM框架下如何實現(xiàn)數(shù)據(jù)從后臺傳輸?shù)角芭_
這篇文章主要介紹了SSM框架下如何實現(xiàn)數(shù)據(jù)從后臺傳輸?shù)角芭_,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05
SpringBoot結合dev-tool實現(xiàn)IDEA項目熱部署的流程步驟
這篇文章主要給大家介紹了SpringBoot結合dev-tool實現(xiàn)IDEA項目熱部署的流程步驟,文章通過圖文介紹的非常詳細,對大家的學習有一定的幫助,需要的朋友可以參考下2023-10-10

