1 package com.foxinmy.weixin4j.util;
2
3 import java.nio.charset.Charset;
4 import java.util.ArrayList;
5 import java.util.Iterator;
6 import java.util.List;
7 import java.util.StringTokenizer;
8
9 public final class StringUtil {
10
11 public static final String EMPTY = "";
12 public static final int INDEX_NOT_FOUND = -1;
13
14 private static byte[] getBytes(final String content, final Charset charset) {
15 if (content == null) {
16 return null;
17 }
18 return content.getBytes(charset);
19 }
20
21 private static String newString(final byte[] bytes, final Charset charset) {
22 return bytes == null ? null : new String(bytes, charset);
23 }
24
25 public static byte[] getBytesUtf8(final String content) {
26 return getBytes(content, Consts.UTF_8);
27 }
28
29 public static String newStringUtf8(final byte[] bytes) {
30 return newString(bytes, Consts.UTF_8);
31 }
32
33 public static boolean isEmpty(final CharSequence cs) {
34 return cs == null || cs.length() == 0;
35 }
36
37 public static boolean isBlank(final CharSequence cs) {
38 int strLen;
39 if (cs == null || (strLen = cs.length()) == 0) {
40 return true;
41 }
42 for (int i = 0; i < strLen; i++) {
43 if (Character.isWhitespace(cs.charAt(i)) == false) {
44 return false;
45 }
46 }
47 return true;
48 }
49
50 public static boolean isNotBlank(final CharSequence cs) {
51 return !isBlank(cs);
52 }
53
54 public static String uncapitalize(final String str) {
55 int strLen;
56 if (str == null || (strLen = str.length()) == 0) {
57 return str;
58 }
59
60 char firstChar = str.charAt(0);
61 if (Character.isLowerCase(firstChar)) {
62
63 return str;
64 }
65
66 return new StringBuilder(strLen).append(Character.toLowerCase(firstChar)).append(str.substring(1)).toString();
67 }
68
69 public static String capitalize(final String str) {
70 int strLen;
71 if (str == null || (strLen = str.length()) == 0) {
72 return str;
73 }
74
75 char firstChar = str.charAt(0);
76 if (Character.isTitleCase(firstChar)) {
77
78 return str;
79 }
80 return new StringBuilder(strLen).append(Character.toTitleCase(firstChar)).append(str.substring(1)).toString();
81 }
82
83 public static String substringBefore(final String str, final String separator) {
84 if (isEmpty(str) || separator == null) {
85 return str;
86 }
87 if (separator.isEmpty()) {
88 return EMPTY;
89 }
90 final int pos = str.indexOf(separator);
91 if (pos == INDEX_NOT_FOUND) {
92 return str;
93 }
94 return str.substring(0, pos);
95 }
96
97 public static String substringAfter(final String str, final String separator) {
98 if (isEmpty(str)) {
99 return str;
100 }
101 if (separator == null) {
102 return EMPTY;
103 }
104 final int pos = str.indexOf(separator);
105 if (pos == INDEX_NOT_FOUND) {
106 return EMPTY;
107 }
108 return str.substring(pos + separator.length());
109 }
110
111 public static String join(final Object[] array, final char separator) {
112 if (array == null) {
113 return null;
114 }
115 return join(array, separator, 0, array.length);
116 }
117
118 public static String join(final Object[] array, final char separator, final int startIndex, final int endIndex) {
119 if (array == null) {
120 return null;
121 }
122 final int noOfItems = endIndex - startIndex;
123 if (noOfItems <= 0) {
124 return EMPTY;
125 }
126 final StringBuilder buf = new StringBuilder(noOfItems * 16);
127 for (int i = startIndex; i < endIndex; i++) {
128 if (i > startIndex) {
129 buf.append(separator);
130 }
131 if (array[i] != null) {
132 buf.append(array[i]);
133 }
134 }
135 return buf.toString();
136 }
137
138 public static String join(final Iterable<?> iterable, final char separator) {
139 if (iterable == null) {
140 return null;
141 }
142 return join(iterable.iterator(), separator);
143 }
144
145 public static String join(final Iterator<?> iterator, final char separator) {
146
147
148 if (iterator == null) {
149 return null;
150 }
151 if (!iterator.hasNext()) {
152 return EMPTY;
153 }
154 final Object first = iterator.next();
155 if (!iterator.hasNext()) {
156 return String.valueOf(first);
157 }
158
159
160 final StringBuilder buf = new StringBuilder(256);
161
162
163 if (first != null) {
164 buf.append(first);
165 }
166
167 while (iterator.hasNext()) {
168 buf.append(separator);
169 final Object obj = iterator.next();
170 if (obj != null) {
171 buf.append(obj);
172 }
173 }
174
175 return buf.toString();
176 }
177
178 public static String join(final int[] array, final char separator) {
179 if (array == null) {
180 return null;
181 }
182 return join(array, separator, 0, array.length);
183 }
184
185 public static String join(final int[] array, final char separator, final int startIndex, final int endIndex) {
186 if (array == null) {
187 return null;
188 }
189 final int noOfItems = endIndex - startIndex;
190 if (noOfItems <= 0) {
191 return EMPTY;
192 }
193 final StringBuilder buf = new StringBuilder(noOfItems * 16);
194 for (int i = startIndex; i < endIndex; i++) {
195 if (i > startIndex) {
196 buf.append(separator);
197 }
198 buf.append(array[i]);
199 }
200 return buf.toString();
201 }
202
203
204
205
206
207 public static String simpleClassName(Object o) {
208 if (o == null) {
209 return "null_object";
210 } else {
211 return simpleClassName(o.getClass());
212 }
213 }
214
215
216
217
218
219 public static String simpleClassName(Class<?> clazz) {
220 if (clazz == null) {
221 return "null_class";
222 }
223
224 Package pkg = clazz.getPackage();
225 if (pkg != null) {
226 return clazz.getName().substring(pkg.getName().length() + 1);
227 } else {
228 return clazz.getName();
229 }
230 }
231
232 public static String[] tokenizeToStringArray(String str, String delimiters) {
233 return tokenizeToStringArray(str, delimiters, true, true);
234 }
235
236 public static String[] tokenizeToStringArray(String str, String delimiters, boolean trimTokens,
237 boolean ignoreEmptyTokens) {
238
239 if (str == null) {
240 return null;
241 }
242 StringTokenizer st = new StringTokenizer(str, delimiters);
243 List<String> tokens = new ArrayList<String>();
244 while (st.hasMoreTokens()) {
245 String token = st.nextToken();
246 if (trimTokens) {
247 token = token.trim();
248 }
249 if (!ignoreEmptyTokens || token.length() > 0) {
250 tokens.add(token);
251 }
252 }
253 return tokens.toArray(new String[tokens.size()]);
254 }
255 }