StringUtil.java
4.6 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
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
191
package cn.living.sharecenter.utils;
import org.springframework.util.StringUtils;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author humeng
* @Description: 沃趣拼团包装的StringUtil
* @date 2019年1月2日 下午3:29:53
*/
public class StringUtil extends StringUtils {
public static String stringToAscii(String value) {
StringBuffer sbu = new StringBuffer();
char[] chars = value.toCharArray();
for (int i = 0; i < chars.length; i++) {
sbu.append((int) chars[i]);
}
return sbu.toString();
}
/**
* @param str
* @return
* @Description: 星化手机号
* @author Jason
* @date May 22, 2018 4:40:03 PM
*/
public static String starFlyPhone(String str) {
if (isPhone(str)) {
return str.substring(0, 3) + "****" + str.substring(7);
} else {
return null;
}
}
/**
* @param str
* @return
* @Description: 格式化字符串
* @author liuyu
* @date 2017年12月11日 上午9:55:17
*/
public static String format(String str) {
return str == null ? "" : str.trim();
}
/**
* @param str
* @param pattern
* @return
* @Description: 正则校验
* @author liuyu
* @date 2017年12月11日 上午9:56:11
*/
public static boolean matche(String str, Pattern pattern) {
return isEmpty(str) ? false : pattern.matcher(str).matches();
}
/**
* @param str
* @return
* @Description: 是否是手机号
* @author liuyu
* @date 2017年12月11日 上午9:56:32
*/
public static boolean isPhone(String str) {
return matche(str, Pattern.compile("^0?1[3|4|5|6|7|8|9][0-9]\\d{8}$"));
}
/**
* @param str
* @return
* @Description: 从字符串中找出数字
* @author Jason
* @date Jul 16, 2018 10:52:25 AM
*/
public static String findNumber(String str) {
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
return matcher.group(0);
}
return null;
}
/**
* @return
* @Description: 随机不重复字符串
* @author liuyu
* @date 2017年12月11日 上午9:57:47
*/
public static String random() {
Random r = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 6; i++) {
int itmp = r.nextInt(26) + 65;
char ctmp = (char) itmp;
sb.append(String.valueOf(ctmp));
}
return System.currentTimeMillis() + sb.toString() + r.nextInt(100);
}
public static String getDate(String format) {
Date date = new Date();
DateFormat fm = new SimpleDateFormat(format);
return fm.format(date);
}
public static String getDate(Date date, String format) {
DateFormat fm = new SimpleDateFormat(format);
return fm.format(date);
}
public static List<String> resolve(String rawData, String regex, int group) {
if (rawData == null) {
return null;
}
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(rawData);
List<String> result = new ArrayList<String>();
try {
while (matcher.find()) {
result.add(matcher.group(group));
}
} catch (Exception e) {
}
return result;
}
public static String resolve2(String rawData, String regex, int group) {
if (rawData == null) {
return null;
}
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(rawData);
String result = null;
try {
if (matcher.find()) {
result = matcher.group(group);
}
} catch (Exception e) {
}
return result;
}
public static boolean stringisEmpty(String value) {
if (value != null && !value.equals("")) {
return false;
} else {
return true;
}
}
/**
* 获取 X 位的随机数
* @param length
* @return
*/
public static String getRandomNumByLength(int length) {
String um ="1";
for (int i=1;i<length;i++){
um+="0";
}
int ramNum= (int) ((Math.random()*9+1)*Integer.parseInt(um));
return String.valueOf(ramNum);
}
}