Blame view

src/main/java/com/huaheng/common/utils/StringUtils.java 11 KB
tangying authored
1
2
package com.huaheng.common.utils;
xqs authored
3
import java.lang.reflect.Field;
tangying authored
4
5
import java.util.Collection;
import java.util.Map;
6
7
import java.util.regex.Matcher;
import java.util.regex.Pattern;
mahuandong authored
8
tangying authored
9
10
11
12
13
import org.apache.commons.lang.text.StrBuilder;
import com.huaheng.common.support.StrFormatter;

/**
 * 字符串工具类
mahuandong authored
14
 *
tangying authored
15
16
 * @author huaheng
 */
mahuandong authored
17
18
19
20
public class StringUtils extends org.apache.commons.lang3.StringUtils {
    /**
     * 空字符串
     */
tangying authored
21
22
    private static final String NULLSTR = "";
mahuandong authored
23
24
25
    /**
     * 下划线
     */
tangying authored
26
27
    private static final char SEPARATOR = '_';
28
29
30
31
32
33
    /**objet  NULL判断非empty*/
    public static boolean isNullNew(Object object)
    {
        return object == null;
    }
tangying authored
34
35
    /**
     * 获取参数不为空值
mahuandong authored
36
     *
tangying authored
37
38
39
     * @param value defaultValue 要判断的value
     * @return value 返回值
     */
mahuandong authored
40
    public static <T> T nvl(T value, T defaultValue) {
tangying authored
41
42
43
44
45
        return value != null ? value : defaultValue;
    }

    /**
     * * 判断一个Collection是否为空, 包含ListSetQueue
mahuandong authored
46
     *
tangying authored
47
48
49
     * @param coll 要判断的Collection
     * @return true:为空 false:非空
     */
mahuandong authored
50
    public static boolean isEmpty(Collection<?> coll) {
51
        return isNullNew(coll) || coll.isEmpty();
tangying authored
52
53
54
55
    }

    /**
     * * 判断一个Collection是否非空,包含ListSetQueue
mahuandong authored
56
     *
tangying authored
57
58
59
     * @param coll 要判断的Collection
     * @return true:非空 false:空
     */
mahuandong authored
60
    public static boolean isNotEmpty(Collection<?> coll) {
tangying authored
61
62
63
64
65
        return !isEmpty(coll);
    }

    /**
     * * 判断一个对象数组是否为空
mahuandong authored
66
     *
tangying authored
67
     * @param objects 要判断的对象数组
mahuandong authored
68
     *                * @return true:为空 false:非空
tangying authored
69
     */
mahuandong authored
70
    public static boolean isEmpty(Object[] objects) {
71
        return isNullNew(objects) || (objects.length == 0);
tangying authored
72
73
74
75
    }

    /**
     * * 判断一个对象数组是否非空
mahuandong authored
76
     *
tangying authored
77
78
79
     * @param objects 要判断的对象数组
     * @return true:非空 false:空
     */
mahuandong authored
80
    public static boolean isNotEmpty(Object[] objects) {
tangying authored
81
82
83
84
85
        return !isEmpty(objects);
    }

    /**
     * * 判断一个Map是否为空
mahuandong authored
86
     *
tangying authored
87
88
89
     * @param map 要判断的Map
     * @return true:为空 false:非空
     */
mahuandong authored
90
    public static boolean isEmpty(Map<?, ?> map) {
91
        return isNullNew(map) || map.isEmpty();
tangying authored
92
93
94
95
    }

    /**
     * * 判断一个Map是否为空
mahuandong authored
96
     *
tangying authored
97
98
99
     * @param map 要判断的Map
     * @return true:非空 false:空
     */
mahuandong authored
100
    public static boolean isNotEmpty(Map<?, ?> map) {
tangying authored
101
102
103
104
105
        return !isEmpty(map);
    }

    /**
     * * 判断一个字符串是否为空串
mahuandong authored
106
     *
tangying authored
107
108
109
     * @param str String
     * @return true:为空 false:非空
     */
mahuandong authored
110
    public static boolean isEmpty(String str) {
111
        return isNullNew(str) || NULLSTR.equals(str.trim());
tangying authored
112
113
114
115
    }

    /**
     * * 判断一个字符串是否为非空串
mahuandong authored
116
     *
tangying authored
117
118
119
     * @param str String
     * @return true:非空串 false:空串
     */
mahuandong authored
120
    public static boolean isNotEmpty(String str) {
tangying authored
121
122
123
124
125
        return !isEmpty(str);
    }

    /**
     * * 判断一个对象是否为空
mahuandong authored
126
     *
tangying authored
127
128
129
     * @param object Object
     * @return true:为空 false:非空
     */
mahuandong authored
130
    public static boolean isNull(Object object) {
xqs authored
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

        if (object == null) {
            return true;
        }
        // 得到类对象
        Class clazz = (Class) object.getClass();
        // 得到所有属性
        Field[] fields = clazz.getDeclaredFields();
        //定义返回结果,默认为true
        boolean flag = true;
        for (Field field : fields) {
            field.setAccessible(true);
            Object fieldValue = null;
            String fieldName = null;
            try {
                //得到属性值
                fieldValue = field.get(object);
                //得到属性类型
                //Type fieldType = field.getGenericType();
                //得到属性名
                fieldName = field.getName();
            } catch (IllegalArgumentException | IllegalAccessException e) {
                e.printStackTrace();
            }
            //只要有一个属性值不为null 就返回false 表示对象不为null 忽略序列化
            if (fieldValue != null && !"serialVersionUID".equals(fieldName)) {
                flag = false;
                break;
            }
        }
        return flag;
tangying authored
162
163
    }
164
165
166
    public static boolean isNull(Object[] objects) {
        return objects == null;
    }
tangying authored
167
168
    /**
     * * 判断一个对象是否非空
mahuandong authored
169
     *
tangying authored
170
171
172
     * @param object Object
     * @return true:非空 false:空
     */
mahuandong authored
173
    public static boolean isNotNull(Object object) {
tangying authored
174
175
176
177
178
        return !isNull(object);
    }

    /**
     * * 判断一个对象是否是数组类型(Java基本型别的数组)
mahuandong authored
179
     *
tangying authored
180
181
182
     * @param object 对象
     * @return true:是数组 false:不是数组
     */
mahuandong authored
183
    public static boolean isArray(Object object) {
184
        return !isNullNew(object) && object.getClass().isArray();
tangying authored
185
186
187
188
189
    }

    /**
     * 去空格
     */
mahuandong authored
190
    public static String trim(String str) {
tangying authored
191
192
193
194
195
        return (str == null ? "" : str.trim());
    }

    /**
     * 截取字符串
mahuandong authored
196
197
     *
     * @param str   字符串
tangying authored
198
199
200
     * @param start 开始
     * @return 结果
     */
mahuandong authored
201
202
    public static String substring(final String str, int start) {
        if (str == null) {
tangying authored
203
204
205
            return NULLSTR;
        }
mahuandong authored
206
        if (start < 0) {
tangying authored
207
208
209
            start = str.length() + start;
        }
mahuandong authored
210
        if (start < 0) {
tangying authored
211
212
            start = 0;
        }
mahuandong authored
213
        if (start > str.length()) {
tangying authored
214
215
216
217
218
219
220
221
            return NULLSTR;
        }

        return str.substring(start);
    }

    /**
     * 截取字符串
mahuandong authored
222
223
     *
     * @param str   字符串
tangying authored
224
     * @param start 开始
mahuandong authored
225
     * @param end   结束
tangying authored
226
227
     * @return 结果
     */
mahuandong authored
228
229
    public static String substring(final String str, int start, int end) {
        if (str == null) {
tangying authored
230
231
232
            return NULLSTR;
        }
mahuandong authored
233
        if (end < 0) {
tangying authored
234
235
            end = str.length() + end;
        }
mahuandong authored
236
        if (start < 0) {
tangying authored
237
238
239
            start = str.length() + start;
        }
mahuandong authored
240
        if (end > str.length()) {
tangying authored
241
242
243
            end = str.length();
        }
mahuandong authored
244
        if (start > end) {
tangying authored
245
246
247
            return NULLSTR;
        }
mahuandong authored
248
        if (start < 0) {
tangying authored
249
250
            start = 0;
        }
mahuandong authored
251
        if (end < 0) {
tangying authored
252
253
254
255
256
257
258
259
260
261
262
263
264
265
            end = 0;
        }

        return str.substring(start, end);
    }

    /**
     * 格式化文本, {} 表示占位符<br>
     * 此方法只是简单将占位符 {} 按照顺序替换为参数<br>
     * 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可<br>
     * 例:<br>
     * 通常使用:format("this is {} for {}", "a", "b") -> this is a for b<br>
     * 转义{} format("this is \\{} for {}", "a", "b") -> this is \{} for a<br>
     * 转义\: format("this is \\\\{} for {}", "a", "b") -> this is \a for b<br>
mahuandong authored
266
     *
tangying authored
267
     * @param template 文本模板,被替换的部分用 {} 表示
mahuandong authored
268
     * @param params   参数值
tangying authored
269
270
     * @return 格式化后的文本
     */
mahuandong authored
271
272
    public static String format(String template, Object... params) {
        if (isEmpty(params) || isEmpty(template)) {
tangying authored
273
274
275
276
277
278
279
280
            return template;
        }
        return StrFormatter.format(template, params);
    }

    /**
     * 驼峰首字符小写
     */
mahuandong authored
281
    public static String uncapitalize(String str) {
tangying authored
282
        int strLen;
mahuandong authored
283
        if (str == null || (strLen = str.length()) == 0) {
tangying authored
284
285
286
287
288
289
290
291
            return str;
        }
        return new StrBuilder(strLen).append(Character.toLowerCase(str.charAt(0))).append(str.substring(1)).toString();
    }

    /**
     * 下划线转驼峰命名
     */
mahuandong authored
292
293
    public static String toUnderScoreCase(String s) {
        if (s == null) {
tangying authored
294
295
296
297
            return null;
        }
        StringBuilder sb = new StringBuilder();
        boolean upperCase = false;
mahuandong authored
298
        for (int i = 0; i < s.length(); i++) {
tangying authored
299
300
301
302
            char c = s.charAt(i);

            boolean nextUpperCase = true;
mahuandong authored
303
            if (i < (s.length() - 1)) {
tangying authored
304
305
306
                nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
            }
mahuandong authored
307
308
            if ((i > 0) && Character.isUpperCase(c)) {
                if (!upperCase || !nextUpperCase) {
tangying authored
309
310
311
                    sb.append(SEPARATOR);
                }
                upperCase = true;
mahuandong authored
312
            } else {
tangying authored
313
314
315
316
317
318
319
320
321
322
323
                upperCase = false;
            }

            sb.append(Character.toLowerCase(c));
        }

        return sb.toString();
    }

    /**
     * 是否包含字符串
mahuandong authored
324
325
     *
     * @param str  验证字符串
tangying authored
326
327
328
     * @param strs 字符串组
     * @return 包含返回true
     */
mahuandong authored
329
330
331
332
    public static boolean inStringIgnoreCase(String str, String... strs) {
        if (str != null && strs != null) {
            for (String s : strs) {
                if (str.equalsIgnoreCase(trim(s))) {
tangying authored
333
334
335
336
337
338
339
340
341
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。 例如:HELLO_WORLD->HelloWorld
mahuandong authored
342
     *
tangying authored
343
344
345
     * @param name 转换前的下划线大写方式命名的字符串
     * @return 转换后的驼峰式命名的字符串
     */
mahuandong authored
346
    public static String convertToCamelCase(String name) {
tangying authored
347
348
        StringBuilder result = new StringBuilder();
        // 快速检查
mahuandong authored
349
        if (name == null || name.isEmpty()) {
tangying authored
350
351
            // 没必要转换
            return "";
mahuandong authored
352
        } else if (!name.contains("_")) {
tangying authored
353
354
355
356
357
            // 不含下划线,仅将首字母大写
            return name.substring(0, 1).toUpperCase() + name.substring(1);
        }
        // 用下划线将原始字符串分割
        String[] camels = name.split("_");
mahuandong authored
358
        for (String camel : camels) {
tangying authored
359
            // 跳过原始字符串中开头、结尾的下换线或双重下划线
mahuandong authored
360
            if (camel.isEmpty()) {
tangying authored
361
362
363
364
365
366
367
368
                continue;
            }
            // 首字母大写
            result.append(camel.substring(0, 1).toUpperCase());
            result.append(camel.substring(1).toLowerCase());
        }
        return result.toString();
    }
mahuandong authored
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

    /**
     * 驼峰式命名法 例如:user_name->userName
     */
    public static String toCamelCase(String s) {
        if (s == null) {
            return null;
        }
        s = s.toLowerCase();
        StringBuilder sb = new StringBuilder(s.length());
        boolean upperCase = false;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);

            if (c == SEPARATOR) {
                upperCase = true;
            } else if (upperCase) {
                sb.append(Character.toUpperCase(c));
                upperCase = false;
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411

    /**
     * 校验一个字符是否是汉字
     *
     * @param str
     *  被校验的字符
     * @return true代表是汉字
     */
    public static boolean isChineseChar(String str) {
        Pattern
                p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher
                m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }
tangying authored
412
}