|
@@ -0,0 +1,203 @@
|
|
1
|
+package com.yunzhi.inte.common;
|
|
2
|
+
|
|
3
|
+import java.io.UnsupportedEncodingException;
|
|
4
|
+import java.net.URLDecoder;
|
|
5
|
+import java.net.URLEncoder;
|
|
6
|
+import java.util.*;
|
|
7
|
+import java.util.regex.Matcher;
|
|
8
|
+import java.util.regex.Pattern;
|
|
9
|
+
|
|
10
|
+public class StringUtils {
|
|
11
|
+ private static Pattern humpPattern = Pattern.compile("[A-Z]");
|
|
12
|
+
|
|
13
|
+ public static boolean isEmpty(String str) {
|
|
14
|
+ return null == str || "".equals(str.trim()) || "null".equals(str) || "undefined".equals(str);
|
|
15
|
+ }
|
|
16
|
+
|
|
17
|
+ public static String trim(String src, String...st) {
|
|
18
|
+ if (null == src) return src;
|
|
19
|
+ if (null == st || st.length == 0) return src.trim();
|
|
20
|
+
|
|
21
|
+ String start = st[0];
|
|
22
|
+ if (!src.startsWith(start)) {
|
|
23
|
+ return src;
|
|
24
|
+ }
|
|
25
|
+
|
|
26
|
+ return src.substring(start.length());
|
|
27
|
+ }
|
|
28
|
+
|
|
29
|
+ public static String trimEnd(String src, String str) {
|
|
30
|
+ if (null == src || null == str) {
|
|
31
|
+ return src;
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+ if (src.endsWith(str)) {
|
|
35
|
+ return src.substring(0, src.length() - str.length());
|
|
36
|
+ }
|
|
37
|
+
|
|
38
|
+ return src;
|
|
39
|
+ }
|
|
40
|
+
|
|
41
|
+ public static String ifNull(String src, String defaultVal) {
|
|
42
|
+ return isEmpty(src) ? defaultVal : src;
|
|
43
|
+ }
|
|
44
|
+
|
|
45
|
+ public static String random(int length) {
|
|
46
|
+ String str="abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
47
|
+ Random random = new Random();
|
|
48
|
+ StringBuffer sb = new StringBuffer();
|
|
49
|
+ for(int i=0;i<length;i++){
|
|
50
|
+ int number=random.nextInt(62);
|
|
51
|
+ sb.append(str.charAt(number));
|
|
52
|
+ }
|
|
53
|
+ return sb.toString();
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ public static String urlEncode(String str) {
|
|
57
|
+ if (isEmpty(str)) return str;
|
|
58
|
+
|
|
59
|
+ try {
|
|
60
|
+ return URLEncoder.encode(str, "UTF-8");
|
|
61
|
+ } catch (UnsupportedEncodingException e) {
|
|
62
|
+ e.printStackTrace();
|
|
63
|
+ return str;
|
|
64
|
+ }
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ public static String urlDecode(String str) {
|
|
68
|
+ if (isEmpty(str)) return str;
|
|
69
|
+
|
|
70
|
+ try {
|
|
71
|
+ return URLDecoder.decode(str, "UTF-8");
|
|
72
|
+ } catch (UnsupportedEncodingException e) {
|
|
73
|
+ e.printStackTrace();
|
|
74
|
+ return str;
|
|
75
|
+ }
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ public static String encodeURIComponent(String str) {
|
|
79
|
+ String result = urlEncode(str);
|
|
80
|
+
|
|
81
|
+ return result.replaceAll("\\+", "%20")
|
|
82
|
+ .replaceAll("\\%21", "!")
|
|
83
|
+ .replaceAll("\\%27", "'")
|
|
84
|
+ .replaceAll("\\%28", "(")
|
|
85
|
+ .replaceAll("\\%29", ")")
|
|
86
|
+ .replaceAll("\\%7E", "~");
|
|
87
|
+ }
|
|
88
|
+
|
|
89
|
+ public static String humpToLine(String str) {
|
|
90
|
+ if (null == str || "".equals(str)) return "";
|
|
91
|
+
|
|
92
|
+ Matcher matcher = humpPattern.matcher(str);
|
|
93
|
+ StringBuffer sb = new StringBuffer();
|
|
94
|
+ while (matcher.find()) {
|
|
95
|
+ matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
|
|
96
|
+ }
|
|
97
|
+ matcher.appendTail(sb);
|
|
98
|
+ return sb.toString();
|
|
99
|
+ }
|
|
100
|
+
|
|
101
|
+ public static String strToUnicode(String str) {
|
|
102
|
+ char[] chars = str.toCharArray();
|
|
103
|
+ String returnStr = "";
|
|
104
|
+ for (int i = 0; i < chars.length; i++) {
|
|
105
|
+ returnStr += "\\u" + Integer.toString(chars[i], 16);
|
|
106
|
+ }
|
|
107
|
+ return returnStr;
|
|
108
|
+ }
|
|
109
|
+
|
|
110
|
+ public static String repeat(String src, int len) {
|
|
111
|
+ if (null == src) {
|
|
112
|
+ return null;
|
|
113
|
+ }
|
|
114
|
+
|
|
115
|
+ if (len <= 0) {
|
|
116
|
+ return src;
|
|
117
|
+ }
|
|
118
|
+
|
|
119
|
+ String res = "";
|
|
120
|
+ for (int i = 0; i < len; i ++) {
|
|
121
|
+ res += src;
|
|
122
|
+ }
|
|
123
|
+
|
|
124
|
+ return res;
|
|
125
|
+ }
|
|
126
|
+
|
|
127
|
+ public static String lpad(String src, String padStr, int len) {
|
|
128
|
+ if (null == src) {
|
|
129
|
+ return null;
|
|
130
|
+ }
|
|
131
|
+
|
|
132
|
+ if (len <= 0 || src.length() >= len) {
|
|
133
|
+ return src;
|
|
134
|
+ }
|
|
135
|
+
|
|
136
|
+ if (null == padStr) {
|
|
137
|
+ padStr = "";
|
|
138
|
+ }
|
|
139
|
+
|
|
140
|
+ int padLen = len - src.length();
|
|
141
|
+ String prefix = repeat(padStr, padLen);
|
|
142
|
+ prefix = prefix.substring(0, padLen);
|
|
143
|
+ return prefix + src;
|
|
144
|
+ }
|
|
145
|
+
|
|
146
|
+ /**
|
|
147
|
+ * 解析 QueryString
|
|
148
|
+ * @param queryStr
|
|
149
|
+ * @return
|
|
150
|
+ */
|
|
151
|
+ public static Map<String, String> parseQueryString(String queryStr) {
|
|
152
|
+ if (isEmpty(queryStr)) {
|
|
153
|
+ return null;
|
|
154
|
+ }
|
|
155
|
+ Map<String, String> result = new HashMap<String, String>();
|
|
156
|
+
|
|
157
|
+ String[] fields = queryStr.split("&");
|
|
158
|
+ for (String field : fields) {
|
|
159
|
+ String[] pairs = field.split("=");
|
|
160
|
+ if (2 != pairs.length) {
|
|
161
|
+ continue;
|
|
162
|
+ }
|
|
163
|
+
|
|
164
|
+ try {
|
|
165
|
+ String key = URLDecoder.decode(pairs[0].trim(), "UTF-8");
|
|
166
|
+ String val = URLDecoder.decode(pairs[1].trim(), "UTF-8");
|
|
167
|
+
|
|
168
|
+ result.put(key, val);
|
|
169
|
+ } catch (UnsupportedEncodingException e) {
|
|
170
|
+ e.printStackTrace();
|
|
171
|
+ }
|
|
172
|
+ }
|
|
173
|
+
|
|
174
|
+ return result;
|
|
175
|
+ }
|
|
176
|
+
|
|
177
|
+ /**
|
|
178
|
+ * Map 转 QueryString
|
|
179
|
+ * @param params
|
|
180
|
+ * @return
|
|
181
|
+ */
|
|
182
|
+ public static String buildQueryString(Map<String, Object> params) {
|
|
183
|
+ if (null == params) {
|
|
184
|
+ return null;
|
|
185
|
+ }
|
|
186
|
+
|
|
187
|
+ List<String> lst = new ArrayList<>();
|
|
188
|
+ for (Map.Entry<String, Object> entry : params.entrySet()) {
|
|
189
|
+ String key = StringUtils.urlEncode(entry.getKey());
|
|
190
|
+ Object o = entry.getValue();
|
|
191
|
+ if (null == o) {
|
|
192
|
+ continue;
|
|
193
|
+ }
|
|
194
|
+ String val = o.toString();
|
|
195
|
+ if (StringUtils.isEmpty(val)) {
|
|
196
|
+ continue;
|
|
197
|
+ }
|
|
198
|
+ String queryStr = key + "=" + StringUtils.urlEncode(val);
|
|
199
|
+ lst.add(queryStr);
|
|
200
|
+ }
|
|
201
|
+ return String.join("&", lst);
|
|
202
|
+ }
|
|
203
|
+}
|