-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathStringUtils.java
More file actions
258 lines (236 loc) · 7.47 KB
/
StringUtils.java
File metadata and controls
258 lines (236 loc) · 7.47 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
package com.xzy.utils.string;
import android.content.res.Resources;
import android.widget.TextView;
import androidx.annotation.ArrayRes;
import androidx.annotation.StringRes;
import com.xzy.utils.common.Utils;
/**
* 字符串相关的工具类。
* 参考 https://github.com/Blankj/AndroidUtilCode/blob/master/lib/utilcode/src/main/
* java/com/blankj/utilcode/util/StringUtils.java
*
* @author xzy
*/
@SuppressWarnings("all")
public final class StringUtils {
private StringUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* Return whether the string is null or 0-length.
*
* @param s The string.
* @return {@code true}: yes<br> {@code false}: no
*/
public static boolean isEmpty(final CharSequence s) {
return s == null || s.length() == 0;
}
/**
* Return whether the string is null or whitespace.
*
* @param s The string.
* @return {@code true}: yes<br> {@code false}: no
*/
public static boolean isTrimEmpty(final String s) {
return (s == null || s.trim().length() == 0);
}
/**
* Return whether the string is null or white space.
*
* @param s The string.
* @return {@code true}: yes<br> {@code false}: no
*/
public static boolean isSpace(final String s) {
if (s == null) return true;
for (int i = 0, len = s.length(); i < len; ++i) {
if (!Character.isWhitespace(s.charAt(i))) {
return false;
}
}
return true;
}
/**
* 检查字符串中是否与待添加的文本有重复内容
*
* @param textView 文本控件
* @param msg 待添加的消息
*/
public static boolean checkRepeat(TextView textView, String msg) {
if (textView.getText().toString().contains(msg)) {
return msg.contains("找到目标设备");
}
return false;
}
/**
* Return whether string1 is equals to string2.
*
* @param s1 The first string.
* @param s2 The second string.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean equals(final CharSequence s1, final CharSequence s2) {
if (s1 == s2) return true;
int length;
if (s1 != null && s2 != null && (length = s1.length()) == s2.length()) {
if (s1 instanceof String && s2 instanceof String) {
return s1.equals(s2);
} else {
for (int i = 0; i < length; i++) {
if (s1.charAt(i) != s2.charAt(i)) return false;
}
return true;
}
}
return false;
}
/**
* Return whether string1 is equals to string2, ignoring case considerations..
*
* @param s1 The first string.
* @param s2 The second string.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean equalsIgnoreCase(final String s1, final String s2) {
return s1 == null ? s2 == null : s1.equalsIgnoreCase(s2);
}
/**
* Return {@code ""} if string equals null.
*
* @param s The string.
* @return {@code ""} if string equals null
*/
public static String null2Length0(final String s) {
return s == null ? "" : s;
}
/**
* Return the length of string.
*
* @param s The string.
* @return the length of string
*/
public static int length(final CharSequence s) {
return s == null ? 0 : s.length();
}
/**
* Set the first letter of string upper.
*
* @param s The string.
* @return the string with first letter upper.
*/
public static String upperFirstLetter(final String s) {
if (s == null || s.length() == 0) return "";
if (!Character.isLowerCase(s.charAt(0))) return s;
return String.valueOf((char) (s.charAt(0) - 32)) + s.substring(1);
}
/**
* Set the first letter of string lower.
*
* @param s The string.
* @return the string with first letter lower.
*/
public static String lowerFirstLetter(final String s) {
if (s == null || s.length() == 0) return "";
if (!Character.isUpperCase(s.charAt(0))) return s;
return String.valueOf((char) (s.charAt(0) + 32)) + s.substring(1);
}
/**
* Reverse the string.
*
* @param s The string.
* @return the reverse string.
*/
public static String reverse(final String s) {
if (s == null) return "";
int len = s.length();
if (len <= 1) return s;
int mid = len >> 1;
char[] chars = s.toCharArray();
char c;
for (int i = 0; i < mid; ++i) {
c = chars[i];
chars[i] = chars[len - i - 1];
chars[len - i - 1] = c;
}
return new String(chars);
}
/**
* Convert string to DBC.
*
* @param s The string.
* @return the DBC string
*/
public static String toDBC(final String s) {
if (s == null || s.length() == 0) return "";
char[] chars = s.toCharArray();
for (int i = 0, len = chars.length; i < len; i++) {
if (chars[i] == 12288) {
chars[i] = ' ';
} else if (65281 <= chars[i] && chars[i] <= 65374) {
chars[i] = (char) (chars[i] - 65248);
} else {
chars[i] = chars[i];
}
}
return new String(chars);
}
/**
* Convert string to SBC.
*
* @param s The string.
* @return the SBC string
*/
public static String toSBC(final String s) {
if (s == null || s.length() == 0) return "";
char[] chars = s.toCharArray();
for (int i = 0, len = chars.length; i < len; i++) {
if (chars[i] == ' ') {
chars[i] = (char) 12288;
} else if (33 <= chars[i] && chars[i] <= 126) {
chars[i] = (char) (chars[i] + 65248);
} else {
chars[i] = chars[i];
}
}
return new String(chars);
}
/**
* Return the string value associated with a particular resource ID.
*
* @param id The desired resource identifier.
* @return the string value associated with a particular resource ID.
*/
public static String getString(@StringRes int id) {
try {
return Utils.getApp().getResources().getString(id);
} catch (Resources.NotFoundException ignore) {
return "";
}
}
/**
* Return the string value associated with a particular resource ID.
*
* @param id The desired resource identifier.
* @param formatArgs The format arguments that will be used for substitution.
* @return the string value associated with a particular resource ID.
*/
public static String getString(@StringRes int id, Object... formatArgs) {
try {
return Utils.getApp().getString(id, formatArgs);
} catch (Resources.NotFoundException ignore) {
return "";
}
}
/**
* Return the string array associated with a particular resource ID.
*
* @param id The desired resource identifier.
* @return The string array associated with the resource.
*/
public static String[] getStringArray(@ArrayRes int id) {
try {
return Utils.getApp().getResources().getStringArray(id);
} catch (Resources.NotFoundException ignore) {
return new String[0];
}
}
}