Blame view

src/main/java/com/huaheng/common/utils/StringUtils.java 10.3 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
    }

    /**
     * * 判断一个对象是否非空
mahuandong authored
158
     *
tangying authored
159
160
161
     * @param object Object
     * @return true:非空 false:空
     */
mahuandong authored
162
    public static boolean isNotNull(Object object) {
tangying authored
163
164
165
166
167
        return !isNull(object);
    }

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

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

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

        return str.substring(start);
    }

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

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

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

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

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

        return sb.toString();
    }

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

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

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