Blame view

src/main/java/com/huaheng/common/utils/StringUtils.java 10.6 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;
mahuandong authored
6
tangying authored
7
8
9
10
11
import org.apache.commons.lang.text.StrBuilder;
import com.huaheng.common.support.StrFormatter;

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

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

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

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

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

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

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

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

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

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

        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
160
161
    }
162
163
164
    public static boolean isNull(Object[] objects) {
        return objects == null;
    }
tangying authored
165
166
    /**
     * * 判断一个对象是否非空
mahuandong authored
167
     *
tangying authored
168
169
170
     * @param object Object
     * @return true:非空 false:空
     */
mahuandong authored
171
    public static boolean isNotNull(Object object) {
tangying authored
172
173
174
175
176
        return !isNull(object);
    }

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

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

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

        return str.substring(start);
    }

    /**
     * 截取字符串
mahuandong authored
220
221
     *
     * @param str   字符串
tangying authored
222
     * @param start 开始
mahuandong authored
223
     * @param end   结束
tangying authored
224
225
     * @return 结果
     */
mahuandong authored
226
227
    public static String substring(final String str, int start, int end) {
        if (str == null) {
tangying authored
228
229
230
            return NULLSTR;
        }
mahuandong authored
231
        if (end < 0) {
tangying authored
232
233
            end = str.length() + end;
        }
mahuandong authored
234
        if (start < 0) {
tangying authored
235
236
237
            start = str.length() + start;
        }
mahuandong authored
238
        if (end > str.length()) {
tangying authored
239
240
241
            end = str.length();
        }
mahuandong authored
242
        if (start > end) {
tangying authored
243
244
245
            return NULLSTR;
        }
mahuandong authored
246
        if (start < 0) {
tangying authored
247
248
            start = 0;
        }
mahuandong authored
249
        if (end < 0) {
tangying authored
250
251
252
253
254
255
256
257
258
259
260
261
262
263
            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
264
     *
tangying authored
265
     * @param template 文本模板,被替换的部分用 {} 表示
mahuandong authored
266
     * @param params   参数值
tangying authored
267
268
     * @return 格式化后的文本
     */
mahuandong authored
269
270
    public static String format(String template, Object... params) {
        if (isEmpty(params) || isEmpty(template)) {
tangying authored
271
272
273
274
275
276
277
278
            return template;
        }
        return StrFormatter.format(template, params);
    }

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

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

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

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

        return sb.toString();
    }

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

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

    /**
     * 驼峰式命名法 例如: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();
    }
tangying authored
392
}