-
Notifications
You must be signed in to change notification settings - Fork 16
/
StringUtil.java
171 lines (154 loc) · 5.37 KB
/
StringUtil.java
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
package com.classic.android.utils;
import android.support.annotation.NonNull;
import android.text.TextUtils;
/**
* 应用名称: BaseProject
* 包 名 称: com.classic.android.utils
*
* 文件描述: 字符串工具类
* 创 建 人: 续写经典
* 创建时间: 2015/11/4 17:26
*/
@SuppressWarnings({"unused", "WeakerAccess"})
public final class StringUtil {
private static final String ELLIPSIS = "...";
private static final String EMPTY = "";
private StringUtil() { }
public static String replaceEmpty(String text) {
return TextUtils.isEmpty(text) ? EMPTY : text;
}
public static String replaceWithMaxLength(@NonNull String text, int maxLength) {
return replaceWithMaxLength(text, maxLength, ELLIPSIS);
}
public static String replaceWithMaxLength(@NonNull String text, int maxLength,
@NonNull String replaceContent) {
return text.length() > maxLength ? (text.substring(0, maxLength) + replaceContent) : text;
}
/**
* 描述:将null转化为“”.
*
* @param str 指定的字符串
* @return 字符串的String类型
*/
public static String parseEmpty(String str) {
if (str == null || "null".equals(str.trim())) {
str = EMPTY;
}
return str.trim();
}
/**
* 描述:判断一个字符串是否为空值.
*
* @param str 指定的字符串
* @return true or false
*/
public static boolean isEmpty(String str) {
return TextUtils.isEmpty(str);
}
/**
* 描述:是否只是字母和数字.
*
* @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 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)) {
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;
}
}
}
return isChinese;
}
/**
* 检测String是否全是中文
*/
public static boolean checkNameChinese(String name) {
boolean res = true;
char[] cTemp = name.toCharArray();
for (int i = 0; i < name.length(); i++) {
if (!isChinese(cTemp[i])) {
res = false;
break;
}
}
return res;
}
/**
* 判定输入汉字是否是中文
*/
public static boolean isChinese(char c) {
for (char param : chineseParam) {
if (param == c) {
return false;
}
}
Character.UnicodeBlock unicodeBlock = Character.UnicodeBlock.of(c);
return (unicodeBlock == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS ||
unicodeBlock == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS ||
unicodeBlock == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A ||
unicodeBlock == Character.UnicodeBlock.GENERAL_PUNCTUATION ||
unicodeBlock == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION ||
unicodeBlock == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS);
}
private static char[] chineseParam = new char[]{'」', ',', '。', '?', '…', ':', '~', '【',
'#', '、', '%', '*', '&', '$', '(', '‘', '’', '“', '”', '『', '〔', '{', '【', '¥',
'£', '‖', '〖', '《', '「', '》', '〗', '】', '}', '〕', '』', '”', ')', '!', ';', '—'};
}