可以使用以下方法:
String org.apache.commons.lang.StringUtils.join(Object[] array, String separator)
//部分源码如下
public static String join(Object[] array, String separator){
if (array == null) {
return null;
}
return join(array, separator, 0, array.length);
}
public static String join(Object[] array, String separator, int startIndex, int endIndex) {
if (array == null) {
return null;
}
if (separator == null) {
separator = "";
}
int bufSize = endIndex - startIndex;
if (bufSize <= 0) {
return "";
}
bufSize *= (((array[startIndex] == null) ? 16 : array[startIndex].toString().length()) + separator.length());
StringBuffer buf = new StringBuffer(bufSize);
for (int i = startIndex; i
if (i > startIndex) {
buf.append(separator);
}
if (array[i] != null) {
buf.append(array[i]);
}
}
return buf.toString();
}
//使用示例:
public static void main(String[] args) {
String[] arr = { "777", "www", "b" };
System.err.println(StringUtils.join(arr, "&"));
//输出777&www&b
}