forked from qiniu/java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringUtils.java
More file actions
147 lines (126 loc) · 3.83 KB
/
Copy pathStringUtils.java
File metadata and controls
147 lines (126 loc) · 3.83 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
package com.qiniu.util;
import com.qiniu.common.Constants;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collection;
/**
* 字符串连接工具类
*/
public final class StringUtils {
private StringUtils() {
}
/**
* @see #join(Object[] array, String sep, String prefix)
*/
public static String join(Object[] array, String sep) {
return join(array, sep, null);
}
/**
* @see #join(Object[] array, String sep, String prefix)
*/
public static String join(Collection list, String sep) {
return join(list, sep, null);
}
/**
* @see #join(Object[] array, String sep, String prefix)
*/
public static String join(Collection list, String sep, String prefix) {
Object[] array = list == null ? null : list.toArray();
return join(array, sep, prefix);
}
/**
* 以指定的分隔符来进行字符串元素连接
* <p>
* 例如有字符串数组array和连接符为逗号(,)
* <code>
* String[] array = new String[] { "hello", "world", "qiniu", "cloud","storage" };
* </code>
* 那么得到的结果是:
* <code>
* hello,world,qiniu,cloud,storage
* </code>
* </p>
*
* @param array 需要连接的对象数组
* @param sep 元素连接之间的分隔符
* @param prefix 前缀字符串
* @return 连接好的新字符串
*/
public static String join(Object[] array, String sep, String prefix) {
if (array == null) {
return "";
}
int arraySize = array.length;
if (arraySize == 0) {
return "";
}
if (sep == null) {
sep = "";
}
if (prefix == null) {
prefix = "";
}
StringBuilder buf = new StringBuilder(prefix);
for (int i = 0; i < arraySize; i++) {
if (i > 0) {
buf.append(sep);
}
buf.append(array[i] == null ? "" : array[i]);
}
return buf.toString();
}
/**
* 以json元素的方式连接字符串中元素
* <p>
* 例如有字符串数组array
* <code>
* String[] array = new String[] { "hello", "world", "qiniu", "cloud","storage" };
* </code>
* 那么得到的结果是:
* <code>
* "hello","world","qiniu","cloud","storage"
* </code>
* </p>
*
* @param array 需要连接的字符串数组
* @return 以json元素方式连接好的新字符串
*/
public static String jsonJoin(String[] array) {
int arraySize = array.length;
int bufSize = arraySize * (array[0].length() + 3);
StringBuilder buf = new StringBuilder(bufSize);
for (int i = 0; i < arraySize; i++) {
if (i > 0) {
buf.append(',');
}
buf.append('"');
buf.append(array[i]);
buf.append('"');
}
return buf.toString();
}
public static boolean isNullOrEmpty(String s) {
return s == null || "".equals(s);
}
public static boolean inStringArray(String s, String[] array) {
for (String x : array) {
if (x.equals(s)) {
return true;
}
}
return false;
}
public static byte[] utf8Bytes(String data) {
return data.getBytes(Constants.UTF_8);
}
public static String utf8String(byte[] data) {
return new String(data, Constants.UTF_8);
}
public static String md5Lower(String src) throws NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("MD5");
digest.update(src.getBytes(Charset.forName("UTF-8")));
byte[] md5Bytes = digest.digest();
return Hex.encodeHexString(md5Bytes);
}
}