UtilString.java
8.01 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package com.taover.util;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class UtilString {
public static String trimByRegexS(String source){
return trimLeftByRegexS(trimRightByRegexS(source));
}
public static String trimLeftByRegexS(String source){
if(source == null){
return null;
}
if(source.equals("")){
return "";
}
Pattern pattern = Pattern.compile("\\S");
int startIndex = -1;
for(int i=0; i<source.length(); ++i){
char data = source.charAt(i);
if(pattern.matcher(data+"").find()){
startIndex = i;
break;
}
}
if(startIndex > -1){
return source.substring(startIndex);
}else{
return "";
}
}
public static String trimRightByRegexS(String source){
if(source == null){
return null;
}
if(source.equals("")){
return "";
}
Pattern pattern = Pattern.compile("\\S");
int endIndex = source.length()-1;
for(int i=source.length()-1; i>=0; --i){
char data = source.charAt(i);
if(pattern.matcher(data+"").find()){
endIndex = i;
break;
}
}
if(endIndex > -1){
return source.substring(0, endIndex+1);
}else{
return "";
}
}
public static String trimByRegexW(String pattern){
Matcher m = Pattern.compile("\\w+").matcher(pattern);
String result = "";
while(m.find()){
String currGroup = m.group();
result += currGroup;
}
return result;
}
public static String trimLeftByRegexW(String pattern){
String tempPattern = new String(pattern);
Pattern p = Pattern.compile("\\w+");
for(int i=0; i<tempPattern.length(); ++i){
if(p.matcher(tempPattern.charAt(i)+"").matches()){
return tempPattern.substring(i);
}
}
return "";
}
public static String trimRightByRegexW(String pattern){
String tempPattern = new String(pattern);
Pattern p = Pattern.compile("\\w+");
for(int i=tempPattern.length()-1; i>=0; --i){
if(p.matcher(tempPattern.charAt(i)+"").matches()){
return tempPattern.substring(0, i+1);
}
}
return "";
}
public static String trimCodePage(String data){
return data.replaceAll(new String(new byte[]{-30, -128, -83}), "");
}
/**
* 在compares字符数组查找pattern字符串,找到则返回字串在数组中的索引,未找到返回-1
* @param pattern
* @param compares
* @return
*/
public static int getStringIndex(String pattern, String compares[]){
//参数检验
if(pattern==null || compares==null){
return -1;
}
//循环遍历compares
for(int i=0; i<compares.length; ++i){
if(compares[i].equals(pattern)){
return i;
}
}
return -1;
}
/**
* 生成发货单的发货单内部序号, 格式:'D'.date('Ymd') +8位数字
* @return
*/
public static String getDeliverySn(){
String result = "D";
String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
long temp = System.currentTimeMillis()%100000000;
result = result + date + temp;
return result;
}
public static String getBeanNameFormTableName(String tableName){
if (tableName == null) return null;
if (tableName.length() == 0) return "";
byte[] ss = tableName.toLowerCase().getBytes();
int len = ss.length;
byte[] ss1 = new byte[len];
ss1[0] = new String(ss, 0).toUpperCase().getBytes()[0];
int k = 1;
for (int i = 1; i < ss.length; i++) {
if (ss[i] == '_') {
if (i < ss.length - 1) {
ss1[k] = (byte)Character.toUpperCase(ss[(i + 1)]);
i++;
}
len--;
} else {
ss1[k] = ss[i];
}
k++;
}
return new String(ss1, 0, len);
}
public static String getCodeWithPreffix(long value, int digitNumber, char preffixChar){
String result = "";
if(value>Long.MAX_VALUE || value<Long.MIN_VALUE){
return null;
}
for(int i=digitNumber; i>0; --i){
long baseDiv = (long) Math.pow(10, i-1);
long baseRand = (long) Math.pow(10, i);
long currentDigit = value % baseRand / baseDiv;
if(currentDigit == 0){
result = result + preffixChar;
}else{
result = result + currentDigit;
}
}
return result;
}
/**
* 将驼峰式命名的字符串转换为下划线大写方式。如果转换前的驼峰式命名的字符串为空,则返回空字符串。</br>
* 例如:HelloWorld->HELLO_WORLD
* @param name 转换前的驼峰式命名的字符串
* @return 转换后下划线大写方式命名的字符串
*/
public static String underscoreName(String name) {
StringBuilder result = new StringBuilder();
if (name != null && name.length() > 0) {
// 将第一个字符处理成大写
result.append(name.substring(0, 1).toUpperCase());
// 循环处理其余字符
for (int i = 1; i < name.length(); i++) {
String s = name.substring(i, i + 1);
// 在大写字母前添加下划线
if (Character.isUpperCase(s.charAt(0)) && Character.isLetter(s.charAt(0))) {
result.append("_");
}
// 其他字符直接转成大写
result.append(s.toUpperCase());
}
}
return result.toString();
}
/**
* 将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。</br>
* 例如:HELLO_WORLD->HelloWorld
* @param name 转换前的下划线大写方式命名的字符串
* @return 转换后的驼峰式命名的字符串
*/
public static String camelName(String name) {
StringBuilder result = new StringBuilder();
// 快速检查
if (name == null || name.isEmpty()) {
// 没必要转换
return "";
} else if (!name.contains("_")) {
// 不含下划线,仅将首字母小写
return name.substring(0, 1).toLowerCase() + name.substring(1);
}
// 用下划线将原始字符串分割
String camels[] = name.split("_");
for (String camel : camels) {
// 跳过原始字符串中开头、结尾的下换线或双重下划线
if (camel.isEmpty()) {
continue;
}
// 处理真正的驼峰片段
if (result.length() == 0) {
// 第一个驼峰片段,全部字母都小写
result.append(camel.toLowerCase());
} else {
// 其他的驼峰片段,首字母大写
result.append(camel.substring(0, 1).toUpperCase());
result.append(camel.substring(1).toLowerCase());
}
}
return result.toString();
}
/**
* 将idstring转换为integer数组
* @param ids
* @param splitStr
* @return
*/
public static Integer[] transIdStringToArray(String ids, String splitStr){
String idArray[] = ids.split(splitStr);
List<Integer> idList = new ArrayList<Integer>();
for(int i=0; i<idArray.length; ++i){
String tempIdStr = idArray[i].trim();
if(!tempIdStr.isEmpty()){
try{
idList.add(Integer.valueOf(tempIdStr));
}catch(Exception e){
e.printStackTrace();
}
}
}
Integer idArrayInt[] = new Integer[idList.size()];
for(int i=0; i<idList.size(); ++i){
idArrayInt[i] = idList.get(i);
}
return idArrayInt;
}
public static void main(String args[]){
// System.out.println(getBeanNameFormTableName("asdf_asdf"));
// System.out.println(System.currentTimeMillis());
// System.out.println(getCodeWithPreffix(1231212, 10, '-'));
// String dd = "\"{\"success\":true,\"code\":1,\"printedorder_id\":\"1654\",\"error_message\":\"\"}\"";
// dd = dd.substring(1, dd.length()-1);
// System.out.println(dd);
//JSONObject temp = JSONObject.fromObject(dd);
//System.out.println(temp.getInt("code"));
String trim = " sd ds sd ";
String trimLeft = " ds sd ";
String trimRight = " ds es &**^";
System.out.println(trimByRegexW(trim));
System.out.println(trimLeftByRegexW(trimLeft));
System.out.println(trimRightByRegexW(trimRight));
}
}