-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringHelper
More file actions
182 lines (172 loc) · 4.5 KB
/
Copy pathStringHelper
File metadata and controls
182 lines (172 loc) · 4.5 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
public class StringHelper {
public final static String TAG = "StringHelper";
private static char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
/**
* @Title: jsonNullToEmpty
* @Description: json解析结果的null转成空字符
* @param str
* @return String
*/
public static String jsonNullToEmpty(String str) {
if (TextUtils.isEmpty(str)) {
return "";
}
if ("null".equalsIgnoreCase(str)) {
return "";
} else {
return str;
}
}
/**
* @Title: isEmail
* @Description: 是否是有效的email地址
* @param email
* @return boolean
*/
public static boolean isEmail(String email) {
Pattern p = Pattern.compile("^[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$");
Matcher m = p.matcher(email);
return m.matches();
}
/**
* @Title: isMobilePhone
* @Description: 是否是有效的手机号码
* @param countryCode
* @param mobiles
* @return boolean
*/
public static final boolean isMobilePhone(String mobile) {
if (TextUtils.isEmpty(mobile)) {
return false;
}
if (mobile.startsWith("+86")) {
mobile = mobile.substring(3);
} else if (mobile.startsWith("+86")) {
mobile = mobile.substring(2);
}
Pattern p = Pattern.compile("^((13[0-9])|(14[0-9])|(15[0-9])|(18[0-9]))\\d{8}$");
Matcher m = p.matcher(mobile);
return m.matches();
}
/**
* @Title: isUrl
* @Description: 是否是有效的网址
* @param url
* @return boolean
*/
public static final boolean isUrl(String url) {
// ^(http(s)?://)?([\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?$
// ^http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$
// ^(http(s)?://)?([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?$
Pattern p = Pattern.compile("^(http(s)?://)?([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?$");
Matcher m = p.matcher(url);
return m.matches();
}
/**
* @Title: byte2HexStr
* @Description: 字节数组转成16进制串
* @param bytes
* @return String
*/
public static String byte2HexStr(byte[] bytes) {
if (bytes == null) {
return "";
}
int num = bytes.length;
char[] chars = new char[num * 2];
int ch;
for (int index = 0; index < num; ++index) {
ch = bytes[index];
// chars[index * 2] = hexDigits[ch >> 4];
// chars[index * 2 + 1] = hexDigits[ch & 0xF];
chars[2 * index] = hexDigits[(ch >> 4) & 0x0f];
chars[2 * index + 1] = hexDigits[ch & 0x0f];
}
return new String(chars);
}
/**
* @Title: str2HexStr
* @Description: 字符串转成16进制字符串
* @param str
* @return String
*/
public static String str2HexStr(String str) {
if (TextUtils.isEmpty(str)) {
return str;
}
StringBuilder sb = new StringBuilder("");
byte[] bytes = str.getBytes();
int bit;
for (int index = 0; index < bytes.length; ++index) {
bit = (bytes[index] & 0x0f0) >> 4;
sb.append(hexDigits[bit]);
bit = bytes[index] & 0x0f;
sb.append(hexDigits[bit]);
}
return sb.toString();
}
/**
* @Title: hexStr2Bytes
* @Description: 16进制字符串转成字节数组
* @param hexStr
* @return byte[]
*/
public static byte[] hexStr2Bytes(String hexStr) {
if (TextUtils.isEmpty(hexStr)) {
return null;
}
if (hexStr.length() % 2 != 0) {
return null;
}
hexStr = hexStr.toUpperCase();
int length = hexStr.length() / 2;
char[] hexChars = hexStr.toCharArray();
byte[] bytes = new byte[length];
for (int index = 0; index < length; ++index) {
int pos = index * 2;
bytes[index] = (byte) (toByte(hexChars[pos]) << 4 | toByte(hexChars[pos + 1]));
}
return bytes;
}
/**
* @Title: md5Hash
* @Description: md5加密
* @param str
* @return String
*/
public static String md5Hash(String str) {
String hashValue = str;
try {
MessageDigest digest = MessageDigest.getInstance("MD5");
digest.update(str.getBytes());
byte[] messageDigest = digest.digest();
hashValue = byte2HexStr(messageDigest);
} catch (NoSuchAlgorithmException e) {
Logger.getInstance(TAG).debug(e.getMessage());
}
return hashValue;
}
private static byte toByte(char ch) {
return (byte) "0123456789ABCDEF".indexOf(ch);
}
/**
* 将字符串里边的半角字符转为全角 ,解决textview参差不齐的情况
* @Title: toDBC
* @Description: TODO
* @param input
* @return
*/
public static String toDBC(String input) {
char[] c = input.toCharArray();
for (int i = 0; i < c.length; i++) {
if (c[i] == 12288) {
c[i] = (char) 32;
continue;
}
if (c[i] > 65280 && c[i] < 65375)
c[i] = (char) (c[i] - 65248);
}
return new String(c);
}
}