From 4da8084e451787cafed959016aec814127f0c7cd Mon Sep 17 00:00:00 2001
From: Stephen Colebourne
The difference from join is that concatenate has no delimiter -- i.e.,
- * StringUtils.concatenate(array) = StringUtils.join(array, null)
.
* StringUtils.concatenate(null) = null * StringUtils.concatenate([]) = "" @@ -1922,11 +1919,36 @@ public static String[] split(String str, String separatorChars, int max) { * * @param array the array of values to concatenate, may be null * @return the concatenated String,null
if null array input + * @deprecated Use the better named {@link #join(Object[])} instead. + * Method will be removed in Commons Lang 3.0. */ public static String concatenate(Object[] array) { return join(array, null); } + /** + *Joins the elements of the provided array into a single String + * containing the provided list of elements.
+ * + *No separator is added to the joined String. + * Null objects or empty strings within the array are represented by + * empty strings.
+ * + *+ * StringUtils.join(null) = null + * StringUtils.join([]) = "" + * StringUtils.join([null]) = "" + * StringUtils.join(["a", "b", "c"]) = "abc" + * StringUtils.join([null, "", "a"]) = "a" + *+ * + * @param array the array of values to join together, may be null + * @return the joined String,null
if null array input + */ + public static String join(Object[] array) { + return join(array, null); + } + /** *Joins the elements of the provided array into a single String * containing the provided list of elements.
diff --git a/src/test/org/apache/commons/lang/StringUtilsTest.java b/src/test/org/apache/commons/lang/StringUtilsTest.java index 7bb36e519..d191198f6 100644 --- a/src/test/org/apache/commons/lang/StringUtilsTest.java +++ b/src/test/org/apache/commons/lang/StringUtilsTest.java @@ -74,7 +74,7 @@ * @author Holger Krauth * @author Henning P. Schmiedehausen * @author Phil Steitz - * @version $Id: StringUtilsTest.java,v 1.41 2003/07/31 20:38:26 scolebourne Exp $ + * @version $Id: StringUtilsTest.java,v 1.42 2003/08/01 21:02:16 scolebourne Exp $ */ public class StringUtilsTest extends TestCase { @@ -205,6 +205,16 @@ public void testCaseFunctions() { "Hello aPACHE", StringUtils.swapCase("hELLO Apache") ); } + public void testJoin_Objectarray() { + assertEquals(null, StringUtils.join(null)); + assertEquals("", StringUtils.join(EMPTY_ARRAY_LIST)); + assertEquals("", StringUtils.join(NULL_ARRAY_LIST)); + assertEquals("abc", StringUtils.join(new String[] {"a", "b", "c"})); + assertEquals("a", StringUtils.join(new String[] {null, "a", ""})); + assertEquals("foo", StringUtils.join(MIXED_ARRAY_LIST)); + assertEquals("foo2", StringUtils.join(MIXED_TYPE_LIST)); + } + public void testJoin_ArrayChar() { assertEquals(null, StringUtils.join((Object[]) null, ',')); assertEquals(TEXT_LIST_CHAR, StringUtils.join(ARRAY_LIST, SEPARATOR_CHAR)); @@ -250,7 +260,7 @@ public void testJoin_IteratorString() { assertEquals(TEXT_LIST, StringUtils.join(Arrays.asList(ARRAY_LIST).iterator(), SEPARATOR)); } - public void testConcatenate_Array() { + public void testConcatenate_Objectarray() { assertEquals(null, StringUtils.concatenate(null)); assertEquals("", StringUtils.concatenate(EMPTY_ARRAY_LIST)); assertEquals("", StringUtils.concatenate(NULL_ARRAY_LIST));