-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringUtils.java
212 lines (191 loc) · 6.85 KB
/
StringUtils.java
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
package com.yifei.demo.util;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
/**
* @author Eephaie
* @version 1.0.0.0
* @date 2018/11/13 10:57
*/
public class StringUtils {
public static final String original = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/";
public static void main(String[] args) {
String s = "中afghi文b测c试d字e符f串g";
s = getChinese(1000);
int len = 100;
String charsetName = "utf8";
try {
List<String> splitedString = subStringb2(s, len, charsetName);
splitedString.forEach(System.out::println);
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
}
}
/**
* 按字节长度截取字符串
*
* @param orgin 需要截取的字符串
* @param blength 需要保留的字节长度
* @param charsetName 编码,对于字符集为UTF-8的数据库,请指定编码为UTF-8;字符集为GBK的数据库,请指定编码GBK
* @return 截取后的字符串
* @throws UnsupportedEncodingException 不支持的字符编码
*/
public static String subStringb(String orgin, int blength, String charsetName) throws UnsupportedEncodingException {
return subStringb2(orgin, blength, charsetName).get(0);
}
/**
* 按字节长度分割字符串
*
* @param orgin 需要截取的字符串
* @param blength 需要保留的字节长度
* @param charsetName 编码,对于字符集为UTF-8的数据库,请指定编码为UTF-8;字符集为GBK的数据库,请指定编码GBK
* @return 分割后的字符串
* @throws UnsupportedEncodingException 不支持的字符编码
*/
public static List<String> subStringb2(String orgin, int blength, String charsetName) throws UnsupportedEncodingException {
List<String> result = new ArrayList<>();
int length;
charsetName = charsetName.toUpperCase();
byte[] bs = orgin.getBytes(charsetName);
while (bs.length > 0) {
length = blength;
if (length >= bs.length) {
result.add(new String(bs, 0, bs.length, charsetName));
break;
}
if ("UTF8".equals(charsetName) || "UTF-8".equals(charsetName)) {
// utf8 encoding
// 0000 0000 - 0000 007F 0xxxxxxx
// 0000 0080 - 0000 07FF 110xxxxx 10xxxxxx
// 0000 0800 - 0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
// 0001 0000 - 0010 FFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
while (length > 0) {
if ((bs[length] | 0x7F) == 0x7F) {
break;
}
if ((bs[length] & 0xC0) == 0xC0) {
break;
}
length--;
}
} else if ("GBK".equals(charsetName)) {
boolean removLast = length % 2 == 1;
for (int i = 0; i < length; i++) {
if ((bs[i] | 0x7F) == 0x7F) {
removLast = !removLast;
}
}
if (removLast) {
length--;
}
} else if ("UNICODE".equals(charsetName)) {
if (length % 2 == 1) {
length--;
}
} else if ("UTF-16".equals(charsetName) || "UTF16".equals(charsetName)) {
if (length % 2 == 1) {
length--;
}
} else if ("UTF-16BE".equals(charsetName)) {
if (length % 2 == 1) {
length--;
}
} else if ("UTF-16LE".equals(charsetName)) {
if (length % 2 == 1) {
length--;
}
}
result.add(new String(bs, 0, length, charsetName));
bs = Arrays.copyOfRange(bs, length, bs.length);
}
if (result.size() == 0) {
result.add("");
}
return result;
}
/**
* 获取随机长度的随机字符串[a-zA-Z0-9\+/]
*
* @param maxLength 字符串最长长度
* @return 随机字符串
*/
public static String getRadomString(int maxLength) {
if (maxLength <= 0) {
throw new IllegalArgumentException("length must be positive integer!");
}
StringBuilder sb = new StringBuilder();
int ol = original.length();
Random seed = new Random();
int length = seed.nextInt(maxLength);
for (int i = 0; i < length; i++) {
char c = original.charAt(seed.nextInt(ol));
sb.append(c);
}
return sb.toString();
}
/**
* 获取固定长度的随机字符串[a-zA-Z0-9\+/]
*
* @param length 字符串长度
* @return 随机字符串
* @throws IllegalArgumentException 输入长度小于等于零
*/
public static String getFixString(int length) throws IllegalArgumentException {
if (length <= 0) {
throw new IllegalArgumentException("length must be positive integer!");
}
StringBuilder sb = new StringBuilder();
int ol = original.length();
Random seed = new Random();
for (int i = 0; i < length; i++) {
char c = original.charAt(seed.nextInt(ol));
sb.append(c);
}
return sb.toString();
}
/**
* 获取随机中文字符串
*
* @param length 所需长度
* @return 随机中文字符串
*/
public static String getChinese(int length) {
StringBuilder sb = new StringBuilder();
// u4e00-u9fa5 中文范围
int min = 0x4e00;
int max = 0x9fa5;
Random seed = new Random();
for (int i = 0; i < length; i++) {
int n = min + seed.nextInt(max - min);
char c = (char) n;
sb.append(Character.toString(c));
}
return sb.toString();
}
/**
* ???
*
* @param dataStr ???
* @return ???
*/
public static String decodeUnicode(final String dataStr) {
int start = 0;
int end = 0;
final StringBuilder buffer = new StringBuilder();
while (start > -1) {
end = dataStr.toLowerCase().indexOf("\\u", start + 2);
String charStr;
if (end == -1) {
charStr = dataStr.substring(start + 2, dataStr.length());
} else {
charStr = dataStr.substring(start + 2, end);
}
char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。
buffer.append(Character.toString(letter));
start = end;
}
return buffer.toString();
}
}