前言
基本语法
首先我们要知道java的基础语法。
1.由26个英文字母大小写,0-9,_或$组成
2.数字不可以开头
3.不可以使用关键字和保留字,但是能包括关键字和保留字
4.Java中严格区分大小写,长度无限制
5.标识符不能包括空格
6.取名尽量做到“见名知意”
驼峰命名法
骆驼式命名法(Camel-Case)又称驼峰式命名法,是电脑程式编写时的一套命名规则(惯例)。
正如它的名称CamelCase所表示的那样,是指混合使用大小写字母来构成变量和函数的名字。
程序员们为了自己的代码能更容易的在同行之间交流,所以多采取统一的可读性比较好的命名方式。
例如,下面是分别用骆驼式命名法和下划线法命名的同一个函数:
printEmployeePaychecks();
print_employee_paychecks();
第一个函数名使用了驼峰命名法——函数名中的每一个逻辑断点都有一个大写字母来标记;
第二个函数名使用了下划线法----函数名中的每一个逻辑断点都有一个下划线来标记。
1.驼峰与下划线互转
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 驼峰法-下划线互转 * @author cshaper * @since 2015.07.04 * @version 1.0.0 */ public class UnderlineToCamelUtils { /** * 下划线转驼峰法 * @param line 源字符串 * @param smallCamel 大小驼峰,是否为小驼峰 * @return 转换后的字符串 */ 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(); } /** * 驼峰法转下划线 * @param line 源字符串 * @return 转换后的字符串 */ 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.测试
1
2
3
4
5
6
|
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.方法补充
除了上面的方法,本文还为大家准备了更简短的方法,需要的可以参考一下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
/*** * <p> * 将驼峰转为下划线 * </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> * 将下划线转为驼峰 * </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实现驼峰和下划线互相转换的示例代码的详细内容,更多关于Java驼峰 下划线互转的资料请关注服务器之家其它相关文章!
原文链接:https://blog.csdn.net/LuckFairyLuckBaby/article/details/89534109