Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 19 additions & 104 deletions src/main/java/com/qiniu/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,108 +13,25 @@ private StringUtils() {
}

/**
* 以指定的分隔符来进行字符串元素连接
* <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 元素连接之间的分隔符
* @return 连接好的新字符串
* @see #join(Object[] array, String sep, String prefix)
*/
public static String join(String[] array, String sep) {
public static String join(Object[] array, String sep) {
return join(array, sep, null);
}

/**
* 以指定的分隔符来进行字符串元素连接
* <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 连接好的新字符串
* @see #join(Object[] array, String sep, String prefix)
*/
public static String join(String[] array, String sep, String prefix) {
if (array == null) {
return null;
}

int arraySize = array.length;
int sepSize = 0;
if (sep != null && !sep.equals("")) {
sepSize = sep.length();
}

int bufSize = (arraySize == 0 ? 0 : ((array[0] == null ? 16 : array[0].length()) + sepSize) * arraySize);
StringBuilder buf = new StringBuilder(bufSize);
if (prefix != null) {
buf.append(prefix);
}
for (int i = 0; i < arraySize; i++) {
if (i > 0) {
buf.append(sep);
}
if (array[i] != null) {
buf.append(array[i]);
}
}
return buf.toString();
public static String join(Collection list, String sep) {
return join(list, sep, null);
}

/**
* 以指定的分隔符来进行字符串列表连接
*
* @param list 需要连接的字符串列表
* @param sep 元素连接之间的分隔符
* @param prefix 前缀字符串
* @return 连接好的新字符串
* @see #join(Object[] array, String sep, String prefix)
*/
public static String join(Collection<String> list, String sep, String prefix) {
if (list == null) {
return null;
}
int arraySize = list.size();
if (arraySize == 0) {
return prefix;
}
int sepSize = 0;
if (sep != null && !sep.equals("")) {
sepSize = sep.length();
}
String first = list.iterator().next();
int bufSize = ((first == null ? 16 : first.length()) + sepSize) * arraySize;
StringBuilder buf = new StringBuilder(bufSize);
if (prefix != null) {
buf.append(prefix);
}
int count = 0;
for (String it : list) {
count++;
if (it != null) {
buf.append(it);
if (count < arraySize) {
buf.append(sep);
}
}
}
return buf.toString();
public static String join(Collection list, String sep, String prefix) {
Object[] array = list == null ? null : list.toArray();
return join(array, sep, prefix);
}

/**
Expand All @@ -137,31 +54,29 @@ public static String join(Collection<String> list, String sep, String prefix) {
*/
public static String join(Object[] array, String sep, String prefix) {
if (array == null) {
return null;
return "";
}

int arraySize = array.length;

if (arraySize == 0) {
return prefix;
return "";
}
int sepSize = 0;
if (sep != null && !sep.equals("")) {
sepSize = sep.length();

if (sep == null) {
sep = "";
}

int bufSize = ((array[0] == null ? 16 : array[0].toString().length()) + sepSize) * arraySize;
StringBuilder buf = new StringBuilder(bufSize);
if (prefix != null) {
buf.append(prefix);
if (prefix == null) {
prefix = "";
}

StringBuilder buf = new StringBuilder(prefix);
for (int i = 0; i < arraySize; i++) {
if (i > 0) {
buf.append(sep);
}
if (array[i] != null) {
buf.append(array[i]);
}
buf.append(array[i] == null ? "" : array[i]);
}
return buf.toString();
}
Expand Down