Bug 17882 submitted [by hand as the patch was out of date].
join and split now take chars as separators as well as Strings. Submitted by: Henning P. Schmiedehausen git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137266 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ae7c590d59
commit
8e54ccc4be
|
@ -77,7 +77,7 @@
|
|||
* @author <a href="mailto:alex@purpletech.com">Alexander Day Chaffee</a>
|
||||
* @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
|
||||
* @since 1.0
|
||||
* @version $Id: StringUtils.java,v 1.34 2003/03/23 04:58:47 bayard Exp $
|
||||
* @version $Id: StringUtils.java,v 1.35 2003/03/23 05:26:23 bayard Exp $
|
||||
*/
|
||||
public class StringUtils {
|
||||
|
||||
|
@ -523,7 +523,7 @@ public static String[] split(String str, String separator, int max) {
|
|||
* @return the concatenated string.
|
||||
*/
|
||||
public static String concatenate(Object[] array) {
|
||||
return join(array, "");
|
||||
return join(array, null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -538,12 +538,40 @@ public static String concatenate(Object[] array) {
|
|||
* @return the joined String
|
||||
*/
|
||||
public static String join(Object[] array, String separator) {
|
||||
if (separator == null) {
|
||||
separator = "";
|
||||
}
|
||||
int arraySize = array.length;
|
||||
int bufSize = (arraySize == 0 ? 0 : (array[0].toString().length() +
|
||||
separator.length()) * arraySize);
|
||||
|
||||
// ArraySize == 0: Len = 0
|
||||
// ArraySize > 0: Len = NofStrings *(len(firstString) + len(separator))
|
||||
// (Assuming that all strings are roughly equally long)
|
||||
int bufSize
|
||||
= ((arraySize == 0) ? 0
|
||||
: arraySize * (array[0].toString().length()
|
||||
+ ((separator != null) ? separator.length(): 0)));
|
||||
|
||||
StringBuffer buf = new StringBuffer(bufSize);
|
||||
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
if ((separator != null) && (i > 0)) {
|
||||
buf.append(separator);
|
||||
}
|
||||
buf.append(array[i]);
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Joins the elements of the provided array into a single String
|
||||
* containing the provided list of elements.</p>
|
||||
*
|
||||
* <p>No delimiter is added before or after the list. A
|
||||
*
|
||||
* @param array the array of values to join together
|
||||
* @param separator the separator character to use
|
||||
* @return the joined String
|
||||
*/
|
||||
public static String join(Object[] array, char separator) {
|
||||
int arraySize = array.length;
|
||||
int bufSize = (arraySize == 0 ? 0 : (array[0].toString().length() + 1) * arraySize);
|
||||
StringBuffer buf = new StringBuffer(bufSize);
|
||||
|
||||
for (int i = 0; i < arraySize; i++) {
|
||||
|
@ -567,9 +595,27 @@ public static String join(Object[] array, String separator) {
|
|||
* @return the joined String
|
||||
*/
|
||||
public static String join(Iterator iterator, String separator) {
|
||||
if (separator == null) {
|
||||
separator = "";
|
||||
}
|
||||
StringBuffer buf = new StringBuffer(256); // Java default is 16, probably too small
|
||||
while (iterator.hasNext()) {
|
||||
buf.append(iterator.next());
|
||||
if ((separator != null) && iterator.hasNext()) {
|
||||
buf.append(separator);
|
||||
}
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Joins the elements of the provided <code>Iterator</code> into
|
||||
* a single String containing the provided elements.</p>
|
||||
*
|
||||
* <p>No delimiter is added before or after the list. A
|
||||
*
|
||||
* @param iterator the <code>Iterator</code> of values to join together
|
||||
* @param separator the separator character to use
|
||||
* @return the joined String
|
||||
*/
|
||||
public static String join(Iterator iterator, char separator) {
|
||||
StringBuffer buf = new StringBuffer(256); // Java default is 16, probably too small
|
||||
while (iterator.hasNext()) {
|
||||
buf.append(iterator.next());
|
||||
|
|
|
@ -68,15 +68,20 @@
|
|||
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
|
||||
* @author <a href="mailto:fredrik@westermarck.com>Fredrik Westermarck</a>
|
||||
* @author Holger Krauth
|
||||
* @version $Id: StringUtilsTest.java,v 1.14 2003/03/17 05:28:37 alex Exp $
|
||||
* @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
|
||||
* @version $Id: StringUtilsTest.java,v 1.15 2003/03/23 05:26:23 bayard Exp $
|
||||
*/
|
||||
public class StringUtilsTest extends TestCase {
|
||||
|
||||
private static final String[] ARRAY_LIST = { "foo", "bar", "baz" };
|
||||
private static final String[] EMPTY_ARRAY_LIST = {};
|
||||
|
||||
private static final String SEPARATOR = ",";
|
||||
private static final char SEPARATOR_CHAR = ';';
|
||||
|
||||
private static final String TEXT_LIST = "foo,bar,baz";
|
||||
private static final String TEXT_LIST_CHAR = "foo;bar;baz";
|
||||
private static final String TEXT_LIST_NOSEP = "foobarbaz";
|
||||
|
||||
private static final String FOO = "foo";
|
||||
private static final String BAR = "bar";
|
||||
|
@ -149,12 +154,44 @@ public void testCaseFunctions() {
|
|||
|
||||
public void testJoin() {
|
||||
assertEquals("concatenate(Object[]) failed",
|
||||
"foobarbaz", StringUtils.concatenate(ARRAY_LIST));
|
||||
TEXT_LIST_NOSEP, StringUtils.concatenate(ARRAY_LIST));
|
||||
assertEquals("join(Object[], String) failed", TEXT_LIST,
|
||||
StringUtils.join(ARRAY_LIST, SEPARATOR));
|
||||
assertEquals("join(Iterator, String) failed", TEXT_LIST,
|
||||
StringUtils.join(Arrays.asList(ARRAY_LIST).iterator(),
|
||||
SEPARATOR));
|
||||
|
||||
assertEquals("join(Object[], char) failed", TEXT_LIST_CHAR,
|
||||
StringUtils.join(ARRAY_LIST, SEPARATOR_CHAR));
|
||||
assertEquals("join(Iterator, char) failed", TEXT_LIST_CHAR,
|
||||
StringUtils.join(Arrays.asList(ARRAY_LIST).iterator(),
|
||||
SEPARATOR_CHAR));
|
||||
|
||||
assertEquals("join(Object[], null) failed", TEXT_LIST_NOSEP,
|
||||
StringUtils.join(ARRAY_LIST, null));
|
||||
assertEquals("join(Iterator, null) failed", TEXT_LIST_NOSEP,
|
||||
StringUtils.join(Arrays.asList(ARRAY_LIST).iterator(),
|
||||
null));
|
||||
|
||||
assertEquals("concatenate(Object[]) failed",
|
||||
"", StringUtils.concatenate(EMPTY_ARRAY_LIST));
|
||||
assertEquals("join(Object[], String) failed", "",
|
||||
StringUtils.join(EMPTY_ARRAY_LIST, SEPARATOR));
|
||||
assertEquals("join(Iterator, String) failed", "",
|
||||
StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST).iterator(),
|
||||
SEPARATOR));
|
||||
|
||||
assertEquals("join(Object[], char) failed", "",
|
||||
StringUtils.join(EMPTY_ARRAY_LIST, SEPARATOR_CHAR));
|
||||
assertEquals("join(Iterator, char) failed", "",
|
||||
StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST).iterator(),
|
||||
SEPARATOR_CHAR));
|
||||
|
||||
assertEquals("join(Object[], null) failed", "",
|
||||
StringUtils.join(EMPTY_ARRAY_LIST, null));
|
||||
assertEquals("join(Iterator, null) failed", "",
|
||||
StringUtils.join(Arrays.asList(EMPTY_ARRAY_LIST).iterator(),
|
||||
null));
|
||||
}
|
||||
|
||||
public void testSplit() {
|
||||
|
|
Loading…
Reference in New Issue