Skip to content

Commit 78bd0b1

Browse files
committed
Merge pull request #156 from simon-liubin/update/stringutil
join([],sep,pre)
2 parents 1128077 + b7d2b14 commit 78bd0b1

File tree

1 file changed

+19
-104
lines changed

1 file changed

+19
-104
lines changed

src/main/java/com/qiniu/util/StringUtils.java

Lines changed: 19 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -13,108 +13,25 @@ private StringUtils() {
1313
}
1414

1515
/**
16-
* 以指定的分隔符来进行字符串元素连接
17-
* <p>
18-
* 例如有字符串数组array和连接符为逗号(,)
19-
* <code>
20-
* String[] array = new String[] { "hello", "world", "qiniu", "cloud","storage" };
21-
* </code>
22-
* 那么得到的结果是:
23-
* <code>
24-
* hello,world,qiniu,cloud,storage
25-
* </code>
26-
* </p>
27-
*
28-
* @param array 需要连接的字符串数组
29-
* @param sep 元素连接之间的分隔符
30-
* @return 连接好的新字符串
16+
* @see #join(Object[] array, String sep, String prefix)
3117
*/
32-
public static String join(String[] array, String sep) {
18+
public static String join(Object[] array, String sep) {
3319
return join(array, sep, null);
3420
}
3521

3622
/**
37-
* 以指定的分隔符来进行字符串元素连接
38-
* <p>
39-
* 例如有字符串数组array和连接符为逗号(,)
40-
* <code>
41-
* String[] array = new String[] { "hello", "world", "qiniu", "cloud","storage" };
42-
* </code>
43-
* 那么得到的结果是:
44-
* <code>
45-
* hello,world,qiniu,cloud,storage
46-
* </code>
47-
* </p>
48-
*
49-
* @param array 需要连接的字符串数组
50-
* @param sep 元素连接之间的分隔符
51-
* @param prefix 前缀字符串
52-
* @return 连接好的新字符串
23+
* @see #join(Object[] array, String sep, String prefix)
5324
*/
54-
public static String join(String[] array, String sep, String prefix) {
55-
if (array == null) {
56-
return null;
57-
}
58-
59-
int arraySize = array.length;
60-
int sepSize = 0;
61-
if (sep != null && !sep.equals("")) {
62-
sepSize = sep.length();
63-
}
64-
65-
int bufSize = (arraySize == 0 ? 0 : ((array[0] == null ? 16 : array[0].length()) + sepSize) * arraySize);
66-
StringBuilder buf = new StringBuilder(bufSize);
67-
if (prefix != null) {
68-
buf.append(prefix);
69-
}
70-
for (int i = 0; i < arraySize; i++) {
71-
if (i > 0) {
72-
buf.append(sep);
73-
}
74-
if (array[i] != null) {
75-
buf.append(array[i]);
76-
}
77-
}
78-
return buf.toString();
25+
public static String join(Collection list, String sep) {
26+
return join(list, sep, null);
7927
}
8028

8129
/**
82-
* 以指定的分隔符来进行字符串列表连接
83-
*
84-
* @param list 需要连接的字符串列表
85-
* @param sep 元素连接之间的分隔符
86-
* @param prefix 前缀字符串
87-
* @return 连接好的新字符串
30+
* @see #join(Object[] array, String sep, String prefix)
8831
*/
89-
public static String join(Collection<String> list, String sep, String prefix) {
90-
if (list == null) {
91-
return null;
92-
}
93-
int arraySize = list.size();
94-
if (arraySize == 0) {
95-
return prefix;
96-
}
97-
int sepSize = 0;
98-
if (sep != null && !sep.equals("")) {
99-
sepSize = sep.length();
100-
}
101-
String first = list.iterator().next();
102-
int bufSize = ((first == null ? 16 : first.length()) + sepSize) * arraySize;
103-
StringBuilder buf = new StringBuilder(bufSize);
104-
if (prefix != null) {
105-
buf.append(prefix);
106-
}
107-
int count = 0;
108-
for (String it : list) {
109-
count++;
110-
if (it != null) {
111-
buf.append(it);
112-
if (count < arraySize) {
113-
buf.append(sep);
114-
}
115-
}
116-
}
117-
return buf.toString();
32+
public static String join(Collection list, String sep, String prefix) {
33+
Object[] array = list == null ? null : list.toArray();
34+
return join(array, sep, prefix);
11835
}
11936

12037
/**
@@ -137,31 +54,29 @@ public static String join(Collection<String> list, String sep, String prefix) {
13754
*/
13855
public static String join(Object[] array, String sep, String prefix) {
13956
if (array == null) {
140-
return null;
57+
return "";
14158
}
14259

14360
int arraySize = array.length;
14461

14562
if (arraySize == 0) {
146-
return prefix;
63+
return "";
14764
}
148-
int sepSize = 0;
149-
if (sep != null && !sep.equals("")) {
150-
sepSize = sep.length();
65+
66+
if (sep == null) {
67+
sep = "";
15168
}
15269

153-
int bufSize = ((array[0] == null ? 16 : array[0].toString().length()) + sepSize) * arraySize;
154-
StringBuilder buf = new StringBuilder(bufSize);
155-
if (prefix != null) {
156-
buf.append(prefix);
70+
if (prefix == null) {
71+
prefix = "";
15772
}
73+
74+
StringBuilder buf = new StringBuilder(prefix);
15875
for (int i = 0; i < arraySize; i++) {
15976
if (i > 0) {
16077
buf.append(sep);
16178
}
162-
if (array[i] != null) {
163-
buf.append(array[i]);
164-
}
79+
buf.append(array[i] == null ? "" : array[i]);
16580
}
16681
return buf.toString();
16782
}

0 commit comments

Comments
 (0)