Blame view

src/main/java/com/huaheng/common/utils/StringUtils.java 10.4 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
26
27
    private static final char SEPARATOR = '_';

    /**
     * 获取参数不为空值
mahuandong authored
28
     *
tangying authored
29
30
31
     * @param value defaultValue 要判断的value
     * @return value 返回值
     */
mahuandong authored
32
    public static <T> T nvl(T value, T defaultValue) {
tangying authored
33
34
35
36
37
        return value != null ? value : defaultValue;
    }

    /**
     * * 判断一个Collection是否为空, 包含ListSetQueue
mahuandong authored
38
     *
tangying authored
39
40
41
     * @param coll 要判断的Collection
     * @return true:为空 false:非空
     */
mahuandong authored
42
    public static boolean isEmpty(Collection<?> coll) {
tangying authored
43
44
45
46
47
        return isNull(coll) || coll.isEmpty();
    }

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

    /**
     * * 判断一个对象数组是否为空
mahuandong authored
58
     *
tangying authored
59
     * @param objects 要判断的对象数组
mahuandong authored
60
     *                * @return true:为空 false:非空
tangying authored
61
     */
mahuandong authored
62
    public static boolean isEmpty(Object[] objects) {
tangying authored
63
64
65
66
67
        return isNull(objects) || (objects.length == 0);
    }

    /**
     * * 判断一个对象数组是否非空
mahuandong authored
68
     *
tangying authored
69
70
71
     * @param objects 要判断的对象数组
     * @return true:非空 false:空
     */
mahuandong authored
72
    public static boolean isNotEmpty(Object[] objects) {
tangying authored
73
74
75
76
77
        return !isEmpty(objects);
    }

    /**
     * * 判断一个Map是否为空
mahuandong authored
78
     *
tangying authored
79
80
81
     * @param map 要判断的Map
     * @return true:为空 false:非空
     */
mahuandong authored
82
    public static boolean isEmpty(Map<?, ?> map) {
tangying authored
83
84
85
86
87
        return isNull(map) || map.isEmpty();
    }

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

    /**
     * * 判断一个字符串是否为空串
mahuandong authored
98
     *
tangying authored
99
100
101
     * @param str String
     * @return true:为空 false:非空
     */
mahuandong authored
102
    public static boolean isEmpty(String str) {
tangying authored
103
104
105
106
107
        return isNull(str) || NULLSTR.equals(str.trim());
    }

    /**
     * * 判断一个字符串是否为非空串
mahuandong authored
108
     *
tangying authored
109
110
111
     * @param str String
     * @return true:非空串 false:空串
     */
mahuandong authored
112
    public static boolean isNotEmpty(String str) {
tangying authored
113
114
115
116
117
        return !isEmpty(str);
    }

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

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

    /**
     * * 判断一个对象是否是数组类型(Java基本型别的数组)
mahuandong authored
171
     *
tangying authored
172
173
174
     * @param object 对象
     * @return true:是数组 false:不是数组
     */
mahuandong authored
175
    public static boolean isArray(Object object) {
tangying authored
176
177
178
179
180
181
        return isNotNull(object) && object.getClass().isArray();
    }

    /**
     * 去空格
     */
mahuandong authored
182
    public static String trim(String str) {
tangying authored
183
184
185
186
187
        return (str == null ? "" : str.trim());
    }

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

        return str.substring(start);
    }

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

    /**
     * 驼峰首字符小写
     */
mahuandong authored
273
    public static String uncapitalize(String str) {
tangying authored
274
        int strLen;
mahuandong authored
275
        if (str == null || (strLen = str.length()) == 0) {
tangying authored
276
277
278
279
280
281
282
283
            return str;
        }
        return new StrBuilder(strLen).append(Character.toLowerCase(str.charAt(0))).append(str.substring(1)).toString();
    }

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

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

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

        return sb.toString();
    }

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

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

    /**
     * 驼峰式命名法 例如: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
386
}