Skip to content

Instantly share code, notes, and snippets.

@quydm
Created December 13, 2016 07:50
Show Gist options
  • Save quydm/ace2c39d8f6deddc386afa2a82224f1f to your computer and use it in GitHub Desktop.
Save quydm/ace2c39d8f6deddc386afa2a82224f1f to your computer and use it in GitHub Desktop.
Improved version of Apache's StringUtils
/**
* @author quydm
*/
public class StringUtils {
public static final String EMPTY_STRING = "";
public static String join(Object[] array, String separator) {
return join(array, separator, 0, array.length);
}
public static String join(Object[] array, String separator, int startIndex, int endIndex) {
if (array == null) {
return null;
}
int noOfItems = endIndex - startIndex;
if (noOfItems <= 0) {
return EMPTY_STRING;
}
if (noOfItems == 1) {
return String.valueOf(array[startIndex]);
}
int newEndIndex = endIndex - 1;
final StringBuilder buf = new StringBuilder();
for (int i = startIndex; i < newEndIndex; i++) {
buf.append(array[i]).append(separator);
}
buf.append(array[newEndIndex]);
return buf.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment