forked from chenssy89/jutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringUtils.java
More file actions
298 lines (272 loc) · 7.82 KB
/
StringUtils.java
File metadata and controls
298 lines (272 loc) · 7.82 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package com.JUtils.base;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* 字符串工具类,对字符串进行常规的处理
*
* @Author:chenssy
* @date:2014年8月5日
*/
public class StringUtils {
/**
* 将半角的符号转换成全角符号.(即英文字符转中文字符)
* @autor:chenssy
* @date:2014年8月7日
*
* @param str
* 要转换的字符
* @return
*/
public static String changeToFull(String str) {
String source = "1234567890!@#$%^&*()abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_=+\\|[];:'\",<.>/?";
String[] decode = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
"!", "@", "#", "$", "%", "︿", "&", "*", "(", ")", "a", "b",
"c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
"o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
"M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X",
"Y", "Z", "-", "_", "=", "+", "\", "|", "【", "】", ";", ":",
"'", "\"", ",", "〈", "。", "〉", "/", "?" };
String result = "";
for (int i = 0; i < str.length(); i++) {
int pos = source.indexOf(str.charAt(i));
if (pos != -1) {
result += decode[pos];
} else {
result += str.charAt(i);
}
}
return result;
}
/**
* 将字符转换为编码为Unicode,格式 为'\u0020'<br>
* unicodeEscaped(' ') = "\u0020"<br>
* unicodeEscaped('A') = "\u0041"
* @autor:chenssy
* @date:2014年8月7日
*
* @param ch
* 待转换的char 字符
* @return
*/
public static String unicodeEscaped(char ch) {
if (ch < 0x10) {
return "\\u000" + Integer.toHexString(ch);
} else if (ch < 0x100) {
return "\\u00" + Integer.toHexString(ch);
} else if (ch < 0x1000) {
return "\\u0" + Integer.toHexString(ch);
}
return "\\u" + Integer.toHexString(ch);
}
/**
* 进行toString操作,若为空,返回默认值
* @autor:chenssy
* @date:2014年8月9日
*
* @param object
* 要进行toString操作的对象
* @param nullStr
* 返回的默认值
* @return
*/
public static String toString(Object object,String nullStr){
return object == null ? nullStr : object.toString();
}
/**
* 将字符串重复N次,null、""不在循环次数里面 <br>
* 当value == null || value == "" return value;<br>
* 当count <= 1 返回 value
* @autor:chenssy
* @date:2014年8月9日
*
* @param value
* 需要循环的字符串
* @param count
* 循环的次数
* @return
*/
public static String repeatString(String value,int count){
if(value == null || "".equals(value) || count <= 1){
return value;
}
int length = value.length();
if(length == 1){ //长度为1,存在字符
return repeatChar(value.charAt(0), count);
}
int outputLength = length * count;
switch (length) {
case 1:
return repeatChar(value.charAt(0), count);
case 2:
char ch0 = value.charAt(0);
char ch1 = value.charAt(1);
char[] output2 = new char[outputLength];
for (int i = count * 2 - 2; i >= 0; i--, i--) {
output2[i] = ch0;
output2[i + 1] = ch1;
}
return new String(output2);
default:
StringBuilder buf = new StringBuilder(outputLength);
for (int i = 0; i < count; i++) {
buf.append(value);
}
return buf.toString();
}
}
/**
* 将某个字符重复N次
* @autor:chenssy
* @date:2014年8月9日
*
* @param ch
* 需要循环的字符
* @param count
* 循环的次数
* @return
*/
public static String repeatChar(char ch, int count) {
char[] buf = new char[count];
for (int i = count - 1; i >= 0; i--) {
buf[i] = ch;
}
return new String(buf);
}
/**
* 判断字符串是否全部都为小写
* @autor:chenssy
* @date:2014年8月9日
*
* @param value
* 待判断的字符串
* @return
*/
public static boolean isAllLowerCase(String value){
if(value == null || "".equals(value)){
return false;
}
for (int i = 0; i < value.length(); i++) {
if (Character.isLowerCase(value.charAt(i)) == false) {
return false;
}
}
return true;
}
/**
* 判断字符串是否全部大写
* @autor:chenssy
* @date:2014年8月9日
*
* @param value
* 待判断的字符串
* @return
*/
public static boolean isAllUpperCase(String value){
if(value == null || "".equals(value)){
return false;
}
for (int i = 0; i < value.length(); i++) {
if (Character.isUpperCase(value.charAt(i)) == false) {
return false;
}
}
return true;
}
/**
* 反转字符串
* @autor:chenssy
* @date:2014年8月9日
*
* @param value
* 待反转的字符串
* @return
*/
public static String reverse(String value){
if(value == null){
return null;
}
return new StringBuffer(value).reverse().toString();
}
/**
* @desc:截取字符串,支持中英文混乱,其中中文当做两位处理
* @autor:chenssy
* @date:2014年8月10日
*
* @param resourceString
* @param length
* @return
*/
public static String subString(String resourceString,int length){
String resultString = "";
if (resourceString == null || "".equals(resourceString) || length < 1) {
return resourceString;
}
if (resourceString.length() < length) {
return resourceString;
}
char[] chr = resourceString.toCharArray();
int strNum = 0;
int strGBKNum = 0;
boolean isHaveDot = false;
for (int i = 0; i < resourceString.length(); i++) {
if (chr[i] >= 0xa1){// 0xa1汉字最小位开始
strNum = strNum + 2;
strGBKNum++;
} else {
strNum++;
}
if (strNum == length || strNum == length + 1) {
if (i + 1 < resourceString.length()) {
isHaveDot = true;
}
break;
}
}
resultString = resourceString.substring(0, strNum - strGBKNum);
if (isHaveDot) {
resultString = resultString + "...";
}
return resultString;
}
/**
*
* @autor:chenssy
* @date:2014年8月10日
*
* @param htmlString
* @param length
* @return
*/
public static String subHTMLString(String htmlString,int length){
return subString(delHTMLTag(htmlString), length);
}
/**
* 过滤html标签,包括script、style、html、空格、回车标签
* @autor:chenssy
* @date:2014年8月10日
*
* @param htmlStr
* @return
*/
public static String delHTMLTag(String htmlStr){
String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
String regEx_space = "\\s*|\t|\r|\n";//定义空格回车换行符
Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
Matcher m_script = p_script.matcher(htmlStr);
htmlStr = m_script.replaceAll(""); // 过滤script标签
Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
Matcher m_style = p_style.matcher(htmlStr);
htmlStr = m_style.replaceAll(""); // 过滤style标签
Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
Matcher m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll(""); // 过滤html标签
Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);
Matcher m_space = p_space.matcher(htmlStr);
htmlStr = m_space.replaceAll(""); // 过滤空格回车标签
return htmlStr.trim(); // 返回文本字符串
}
}