Created
December 13, 2016 07:50
-
-
Save quydm/ace2c39d8f6deddc386afa2a82224f1f to your computer and use it in GitHub Desktop.
Improved version of Apache's StringUtils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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