CharUtils.java
3.71 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package com.yoho.datasync.producer.common.utils;
import org.apache.commons.lang3.StringUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CharUtils {
/**
* 将字符转换成大写,如a->A
*
* @param c
* @return
*/
public static char toUpperCase(char c) {
if (c < 97 || c > 122)
return c;
else
return (char) (c - 32);
}
/**
* 将字符转换成小写,如A->a
*
* @param c
* @return
*/
public static char toLowerCase(char c) {
if (c < 65 || c > 90)
return c;
else
return (char) (c + 32);
}
public static boolean isLowerCase(char c) {
return (c >= 'a' && c <= 'z');
}
public static boolean isUpperCase(char c) {
return (c >= 'A' && c <= 'Z');
}
public static String getLowerCaseNames(String names) {
StringBuilder sb = new StringBuilder();
char[] cc = names.toCharArray();
sb.append(toLowerCase(cc[0]));
for (int i = 1; i < cc.length; i++) {
if (isLowerCase(cc[i - 1]) && isUpperCase(cc[i])) {
sb.append("_" + toLowerCase(cc[i]));
} else {
sb.append(toLowerCase(cc[i]));
}
}
;
return sb.toString();
}
/**
* 将驼峰风格替换为下划线风格
*
* @param str
* @return
*/
public static String CamelhumpToUnderline(String str) {
Matcher matcher = Pattern.compile("[A-Z]").matcher(str);
StringBuilder builder = new StringBuilder(str);
for (int i = 0; matcher.find(); i++) {
builder.replace(matcher.start() + i, matcher.end() + i, "_" + matcher.group().toLowerCase());
}
if (builder.charAt(0) == '_') {
builder.deleteCharAt(0);
}
return builder.toString();
}
/**
* 将下划线风格替换为驼峰风格
*
* @param str
* @return
*/
public static String underlineToCamelhump(String str) {
Matcher matcher = Pattern.compile("_[a-z]").matcher(str);
StringBuilder builder = new StringBuilder(str);
for (int i = 0; matcher.find(); i++) {
builder.replace(matcher.start() - i, matcher.end() - i, matcher.group().substring(1).toUpperCase());
}
if (Character.isUpperCase(builder.charAt(0))) {
builder.replace(0, 1, String.valueOf(Character.toLowerCase(builder.charAt(0))));
}
return builder.toString();
}
public static String underlineToCamelhumpWithNumber(String str) {
Matcher matcher = Pattern.compile("_[a-z0-9]").matcher(str);
StringBuilder builder = new StringBuilder(str);
for (int i = 0; matcher.find(); i++) {
builder.replace(matcher.start() - i, matcher.end() - i, matcher.group().substring(1).toUpperCase());
}
if (Character.isUpperCase(builder.charAt(0))) {
builder.replace(0, 1, String.valueOf(Character.toLowerCase(builder.charAt(0))));
}
return builder.toString();
}
public static String getBeanName(String str) {
StringBuilder builder = new StringBuilder(str);
builder.replace(0, 1, String.valueOf(Character.toUpperCase(builder.charAt(0))));
return builder.toString();
}
public static boolean isNumeric(String str) {
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
}
public static String standardized(String str){
if(StringUtils.isEmpty(str)){
return str;
}
return str.trim().toLowerCase().replaceAll(" ", "");
}
public static void main(String[] args) {
System.out.println(isLowerCase('C'));
String s1 = "size_attribute";
System.out.println(underlineToCamelhump(s1));
String s2 = "hserInfo";
System.out.println(getLowerCaseNames(s2));
String s3 = "brand";
System.out.println(getBeanName(s3));
String s4 = "skn_7_days_view";
System.out.println(underlineToCamelhumpWithNumber(s4));
System.out.println(standardized("Vans 卫衣"));
}
}