StringUtil.java
3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* COPYRIGHT. ShenZhen JiMi Technology Co., Ltd. 2017.
* ALL RIGHTS RESERVED.
*
* No part of this publication may be reproduced, stored in a retrieval system, or transmitted,
* on any form or by any means, electronic, mechanical, photocopying, recording,
* or otherwise, without the prior written permission of ShenZhen JiMi Network Technology Co., Ltd.
*
* Amendment History:
*
* Date By Description
* ------------------- ----------- -------------------------------------------
* 2017年4月5日 yaojianping Create the class
* http://www.jimilab.com/
*/
package com.jimi.openapi.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @FileName StringUtil.java
* @Description:
*
* @Date 2017年4月5日 下午5:36:34
* @author yaojianping
* @version 1.0
*/
public final class StringUtil {
public static boolean areNotEmpty(String... values) {
boolean result = true;
if ((values == null) || (values.length == 0))
result = false;
else {
for (String value : values) {
result &= !isEmpty(value);
}
}
return result;
}
public static boolean isEmpty(String value) {
int strLen;
if ((value == null) || ((strLen = value.length()) == 0))
return true;
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(value.charAt(i))) {
return false;
}
}
return true;
}
public static final char UNDERLINE = '_';
/**
* 驼峰格式字符串转换为下划线格式字符串
*
* @param param
* @return
*/
public static String camelToUnderline(String param) {
if (param == null || "".equals(param.trim())) {
return "";
}
int len = param.length();
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; i++) {
char c = param.charAt(i);
if (Character.isUpperCase(c)) {
sb.append(UNDERLINE);
sb.append(Character.toLowerCase(c));
} else {
sb.append(c);
}
}
return sb.toString();
}
/**
* 下划线格式字符串转换为驼峰格式字符串
*
* @param param
* @return
*/
public static String underlineToCamel(String param) {
if (param == null || "".equals(param.trim())) {
return "";
}
int len = param.length();
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; i++) {
char c = param.charAt(i);
if (c == UNDERLINE) {
if (++i < len) {
sb.append(Character.toUpperCase(param.charAt(i)));
}
} else {
sb.append(c);
}
}
return sb.toString();
}
/**
* 下划线格式字符串转换为驼峰格式字符串2
*
* @param param
* @return
*/
public static String underlineToCamel2(String param) {
if (param == null || "".equals(param.trim())) {
return "";
}
StringBuilder sb = new StringBuilder(param);
Matcher mc = Pattern.compile("_").matcher(param);
int i = 0;
while (mc.find()) {
int position = mc.end() - (i++);
sb.replace(position - 1, position + 1, sb.substring(position, position + 1).toUpperCase());
}
return sb.toString();
}
public static void main(String[] args) {
String aaa = "app_version_fld";
System.out.println(underlineToCamel(aaa));
System.out.println(underlineToCamel2(aaa));
aaa = "appVersionFld";
System.out.println(camelToUnderline(aaa));
}
}