-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathAbStrUtil.java
More file actions
525 lines (492 loc) · 15.4 KB
/
AbStrUtil.java
File metadata and controls
525 lines (492 loc) · 15.4 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
* Copyright (C) 2012 www.amsoft.cn
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ab.util;
import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
// TODO: Auto-generated Javadoc
/**
* © 2012 amsoft.cn
* 名称:AbStrUtil.java
* 描述:字符串处理类.
*/
public class AbStrUtil {
/**
* 给填写搜索单词的关键词显示 特殊颜色
*
* @param word
* @return
*/
public static SpannableStringBuilder changeTextColor(String word, String input, int color, Context context) {
int len = input.length();
int start = 0;
int end = 0;
int position = 0;
SpannableStringBuilder style = new SpannableStringBuilder(word);
while (true) {
position = word.indexOf(input, end);
if (-1 == position) {
break;
}
start = position;
end = start + len;
style.setSpan(new ForegroundColorSpan(context.getResources().getColor(color)), start, end,
Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
}
return style;
}
/**
* 描述:将null转化为“”.
*
* @param str 指定的字符串
* @return 字符串的String类型
*/
public static String parseEmpty(String str) {
if (str == null || "null".equals(str.trim())) {
str = "";
}
return str.trim();
}
/**
* 描述:判断一个字符串是否为null或空值.
*
* @param str 指定的字符串
* @return true or false
*/
public static boolean isEmpty(String str) {
return str == null || str.trim().length() == 0 || str.equals("null");
}
/**
* 集合是否为空
*
* @param list
* @return
*/
public static boolean isEmpty(List list) {
if (list != null && list.size() > 0) {
return false;
}
return true;
}
/**
* 获取字符串中文字符的长度(每个中文算2个字符).
*
* @param str 指定的字符串
* @return 中文字符的长度
*/
public static int chineseLength(String str) {
int valueLength = 0;
String chinese = "[\u0391-\uFFE5]";
/* 获取字段值的长度,如果含中文字符,则每个中文字符长度为2,否则为1 */
if (!isEmpty(str)) {
for (int i = 0; i < str.length(); i++) {
/* 获取一个字符 */
String temp = str.substring(i, i + 1);
/* 判断是否为中文字符 */
if (temp.matches(chinese)) {
valueLength += 2;
}
}
}
return valueLength;
}
/**
* 描述:获取字符串的长度.
*
* @param str 指定的字符串
* @return 字符串的长度(中文字符计2个)
*/
public static int strLength(String str) {
int valueLength = 0;
String chinese = "[\u0391-\uFFE5]";
if (!isEmpty(str)) {
//获取字段值的长度,如果含中文字符,则每个中文字符长度为2,否则为1
for (int i = 0; i < str.length(); i++) {
//获取一个字符
String temp = str.substring(i, i + 1);
//判断是否为中文字符
if (temp.matches(chinese)) {
//中文字符长度为2
valueLength += 2;
} else {
//其他字符长度为1
valueLength += 1;
}
}
}
return valueLength;
}
/**
* 描述:获取指定长度的字符所在位置.
*
* @param str 指定的字符串
* @param maxL 要取到的长度(字符长度,中文字符计2个)
* @return 字符的所在位置
*/
public static int subStringLength(String str, int maxL) {
int currentIndex = 0;
int valueLength = 0;
String chinese = "[\u0391-\uFFE5]";
//获取字段值的长度,如果含中文字符,则每个中文字符长度为2,否则为1
for (int i = 0; i < str.length(); i++) {
//获取一个字符
String temp = str.substring(i, i + 1);
//判断是否为中文字符
if (temp.matches(chinese)) {
//中文字符长度为2
valueLength += 2;
} else {
//其他字符长度为1
valueLength += 1;
}
if (valueLength >= maxL) {
currentIndex = i;
break;
}
}
return currentIndex;
}
/**
* 描述:手机号格式验证.
*
* @param str 指定的手机号码字符串
* @return 是否为手机号码格式:是为true,否则false
*/
public static Boolean isMobileNo(String str) {
Boolean isMobileNo = false;
try {
Pattern p = Pattern.compile("^((13[0-9])|(15[0-9])|(18[0-9])|(14[0-9])|(17[0-9]))\\d{8}$");
Matcher m = p.matcher(str);
isMobileNo = m.matches();
} catch (Exception e) {
e.printStackTrace();
}
return isMobileNo;
}
/**
* 描述:是否只是字母和数字.
*
* @param str 指定的字符串
* @return 是否只是字母和数字:是为true,否则false
*/
public static Boolean isNumberLetter(String str) {
Boolean isNoLetter = false;
String expr = "^[A-Za-z0-9]+$";
if (str.matches(expr)) {
isNoLetter = true;
}
return isNoLetter;
}
/**
* 描述:是否只是数字.
*
* @param str 指定的字符串
* @return 是否只是数字:是为true,否则false
*/
public static Boolean isNumber(String str) {
Boolean isNumber = false;
String expr = "^[0-9]+$";
if (str.matches(expr)) {
isNumber = true;
}
return isNumber;
}
/**
* 描述:是否是邮箱.
*
* @param str 指定的字符串
* @return 是否是邮箱:是为true,否则false
*/
public static Boolean isEmail(String str) {
Boolean isEmail = false;
String expr = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
if (str.matches(expr)) {
isEmail = true;
}
return isEmail;
}
/**
* 描述:是否是中文.
*
* @param str 指定的字符串
* @return 是否是中文:是为true,否则false
*/
public static Boolean isChinese(String str) {
Boolean isChinese = true;
String chinese = "[\u0391-\uFFE5]";
if (!isEmpty(str)) {
//获取字段值的长度,如果含中文字符,则每个中文字符长度为2,否则为1
for (int i = 0; i < str.length(); i++) {
//获取一个字符
String temp = str.substring(i, i + 1);
//判断是否为中文字符
if (temp.matches(chinese)) {
} else {
isChinese = false;
}
}
}
return isChinese;
}
/**
* 描述:是否包含中文.
*
* @param str 指定的字符串
* @return 是否包含中文:是为true,否则false
*/
public static Boolean isContainChinese(String str) {
Boolean isChinese = false;
String chinese = "[\u0391-\uFFE5]";
if (!isEmpty(str)) {
//获取字段值的长度,如果含中文字符,则每个中文字符长度为2,否则为1
for (int i = 0; i < str.length(); i++) {
//获取一个字符
String temp = str.substring(i, i + 1);
//判断是否为中文字符
if (temp.matches(chinese)) {
isChinese = true;
} else {
}
}
}
return isChinese;
}
/**
* 是否包含中文数字字母的用户名
* 长度1-20
*
* @param str
* @return
*/
public static boolean isConintChinseUser(String str) {
/**此正则表达式将上面二者结合起来进行判断,中文、大小写字母和数字**/
String all = "^[\\u4E00-\\u9FA5\\uF900-\\uFA2D\\w]{2,10}$";
Pattern pattern = Pattern.compile(all);
return pattern.matcher(str).matches();
}
/**
* 描述:从输入流中获得String.
*
* @param is 输入流
* @return 获得的String
*/
public static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
//最后一个\n删除
if (sb.indexOf("\n") != -1 && sb.lastIndexOf("\n") == sb.length() - 1) {
sb.delete(sb.lastIndexOf("\n"), sb.lastIndexOf("\n") + 1);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
/**
* 描述:标准化日期时间类型的数据,不足两位的补0.
*
* @param dateTime 预格式的时间字符串,如:2012-3-2 12:2:20
* @return String 格式化好的时间字符串,如:2012-03-20 12:02:20
*/
public static String dateTimeFormat(String dateTime) {
StringBuilder sb = new StringBuilder();
try {
if (isEmpty(dateTime)) {
return null;
}
String[] dateAndTime = dateTime.split(" ");
if (dateAndTime.length > 0) {
for (String str : dateAndTime) {
if (str.indexOf("-") != -1) {
String[] date = str.split("-");
for (int i = 0; i < date.length; i++) {
String str1 = date[i];
sb.append(strFormat2(str1));
if (i < date.length - 1) {
sb.append("-");
}
}
} else if (str.indexOf(":") != -1) {
sb.append(" ");
String[] date = str.split(":");
for (int i = 0; i < date.length; i++) {
String str1 = date[i];
sb.append(strFormat2(str1));
if (i < date.length - 1) {
sb.append(":");
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return sb.toString();
}
/**
* 描述:不足2个字符的在前面补“0”.
*
* @param str 指定的字符串
* @return 至少2个字符的字符串
*/
public static String strFormat2(String str) {
try {
if (str.length() <= 1) {
str = "0" + str;
}
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
/**
* 描述:截取字符串到指定字节长度.
*
* @param str the str
* @param length 指定字节长度
* @return 截取后的字符串
*/
public static String cutString(String str, int length) {
return cutString(str, length, "");
}
/**
* 描述:截取字符串到指定字节长度.
*
* @param str 文本
* @param length 字节长度
* @param dot 省略符号
* @return 截取后的字符串
*/
public static String cutString(String str, int length, String dot) {
int strBLen = strlen(str, "GBK");
if (strBLen <= length) {
return str;
}
int temp = 0;
StringBuffer sb = new StringBuffer(length);
char[] ch = str.toCharArray();
for (char c : ch) {
sb.append(c);
if (c > 256) {
temp += 2;
} else {
temp += 1;
}
if (temp >= length) {
if (dot != null) {
sb.append(dot);
}
break;
}
}
return sb.toString();
}
/**
* 描述:截取字符串从第一个指定字符.
*
* @param str1 原文本
* @param str2 指定字符
* @param offset 偏移的索引
* @return 截取后的字符串
*/
public static String cutStringFromChar(String str1, String str2, int offset) {
if (isEmpty(str1)) {
return "";
}
int start = str1.indexOf(str2);
if (start != -1) {
if (str1.length() > start + offset) {
return str1.substring(start + offset);
}
}
return "";
}
/**
* 描述:获取字节长度.
*
* @param str 文本
* @param charset 字符集(GBK)
* @return the int
*/
public static int strlen(String str, String charset) {
if (str == null || str.length() == 0) {
return 0;
}
int length = 0;
try {
length = str.getBytes(charset).length;
} catch (Exception e) {
e.printStackTrace();
}
return length;
}
/**
* 获取大小的描述.
*
* @param size 字节个数
* @return 大小的描述
*/
public static String getSizeDesc(long size) {
String suffix = "B";
if (size >= 1024) {
suffix = "K";
size = size >> 10;
if (size >= 1024) {
suffix = "M";
//size /= 1024;
size = size >> 10;
if (size >= 1024) {
suffix = "G";
size = size >> 10;
//size /= 1024;
}
}
}
return size + suffix;
}
/**
* 描述:ip地址转换为10进制数.
*
* @param ip the ip
* @return the long
*/
public static long ip2int(String ip) {
ip = ip.replace(".", ",");
String[] items = ip.split(",");
return Long.valueOf(items[0]) << 24 | Long.valueOf(items[1]) << 16 | Long.valueOf(items[2]) << 8 | Long.valueOf(items[3]);
}
}