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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package com.yiboshi.science.utils;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* /**
* class: 下换线/驼峰命名转换$
* create by: wxl$
* description:
* create time: $
*
* @param: $
*/
public class StringUtil {
/**
* 下划线命名转驼峰命名
*
* @param underscore
* @return
*/
public static String underscoreToCamelCase(String underscore) {
String[] ss = underscore.split("_");
if (ss.length == 1) {
return underscore;
}
StringBuffer sb = new StringBuffer();
sb.append(ss[0]);
for (int i = 1; i < ss.length; i++) {
sb.append(upperFirstCase(ss[i]));
}
return sb.toString();
}
/**
* 驼峰 转下划线
*
* @param camelCase
* @return
*/
public static String toLine(String camelCase) {
Pattern humpPattern = Pattern.compile("[A-Z]");
Matcher matcher = humpPattern.matcher(camelCase);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
}
matcher.appendTail(sb);
return sb.toString();
}
/**
* 首字母 转小写
*
* @param str
* @return
*/
private static String lowerFirstCase(String str) {
char[] chars = str.toCharArray();
chars[0] += 32;
return String.valueOf(chars);
}
/**
* 首字母 转大写
*
* @param str
* @return
*/
private static String upperFirstCase(String str) {
char[] chars = str.toCharArray();
chars[0] -= 32;
return String.valueOf(chars);
}
public static void main(String[] args) {
String camelCase = StringUtil.underscoreToCamelCase("cteate_time");
System.out.println(camelCase);//cteateTime
System.out.println(toLine("cteateTimeAndUser"));//cteate_time_and_user
}
public static boolean arrEquals(String[] arr, Integer targetValue) {
for (String s : arr) {
if (s.equals(targetValue.toString()))
return true;
}
return false;
}
public static boolean isContainsRole(String roleArr, String Role) {
String[] arr = roleArr.split(",");
int a = Arrays.binarySearch(arr, Role);
if (a >= 0) {
return true;
} else {
return false;
}
}
/**
* @param t1 用于赋值的实体类
* @param t2 需要待赋值的实体类
*/
public static Object copyObj2Obj(Object t1, Object t2) {
Class ct1 = (Class) t1.getClass();
Class ct2 = (Class) t2.getClass();
Field[] fs = ct1.getDeclaredFields();
Field[] nfs = ct2.getDeclaredFields();
for (int i = 0; i < fs.length; i++) {
Field f = fs[i];
f.setAccessible(true); //设置些属性是可以访问的
Object val = new Object();
try {
val = f.get(t1);//得到此属性的值
} catch (IllegalAccessException e) {
e.printStackTrace();
}
for (int j = 0; j < nfs.length; j++) {
Field nf = nfs[j];
nf.setAccessible(true); //设置些属性是可以访问的
if (f.getName().equals(nf.getName())) {
try {
nf.set(t2, val);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
return t2;
}
/**
* 隐藏手机号
*
* @param phoneNum
* @return
*/
public static String hideAllPhoneNum(String phoneNum) {
if (phoneNum != null && !phoneNum.equals("")) {
Pattern pattern = Pattern.compile("((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}");
Matcher matcher = pattern.matcher(phoneNum);
StringBuffer sb = new StringBuffer();
try {
while (matcher.find()) {
String phoneStr = matcher.group();
phoneStr = phoneStr.substring(0, 3) + "****" + phoneStr.substring(7, phoneStr.length());
matcher.appendReplacement(sb, phoneStr);
}
matcher.appendTail(sb);
} catch (Exception ex) {
ex.printStackTrace();
}
return sb.toString();
} else return null;
}
/**
* 隐藏身份证号码
*
* @param idCardNum
* @return
*/
public static String hideAllIdCardNum(String idCardNum) {
if (idCardNum != null && !idCardNum.equals("")) {
Pattern pattern = Pattern.compile("(\\d{6})(19|20)(\\d{2})(1[0-2]|0[1-9])(0[1-9]|[1-2][0-9]|3[0-1])(\\d{3})(\\d|X|x)");
Matcher matcher = pattern.matcher(idCardNum);
StringBuffer sb = new StringBuffer();
try {
while (matcher.find()) {
String idCardStr = matcher.group();
int len = idCardStr.length();
if (len >= 9) {
idCardStr = idCardStr.replaceAll("(.{" + (len < 12 ? 3 : 8) + "})(.*)(.{2})", "$1" + "********" + "$3");
}
matcher.appendReplacement(sb, idCardStr);
}
matcher.appendTail(sb);
} catch (Exception ex) {
ex.printStackTrace();
}
return sb.toString();
} else return null;
}
}