You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ba...@apache.org on 2009/10/20 07:18:57 UTC
svn commit: r826955 - in /commons/proper/lang/trunk/src:
java/org/apache/commons/lang/StringUtils.java
test/org/apache/commons/lang/StringUtilsTest.java
Author: bayard
Date: Tue Oct 20 05:18:57 2009
New Revision: 826955
URL: http://svn.apache.org/viewvc?rev=826955&view=rev
Log:
Adding a StringUtils.repeat(String, String separator, int) method per request in LANG-348
Modified:
commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java
commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java
Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java?rev=826955&r1=826954&r2=826955&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java Tue Oct 20 05:18:57 2009
@@ -4018,6 +4018,35 @@
}
/**
+ * <p>Repeat a String <code>repeat</code> times to form a
+ * new String, with a String separator injected each time. </p>
+ *
+ * <pre>
+ * StringUtils.repeat(null, null, 2) = null
+ * StringUtils.repeat(null, "x", 2) = null
+ * StringUtils.repeat("", null, 0) = ""
+ * StringUtils.repeat("", "", 2) = ""
+ * StringUtils.repeat("", "x", 3) = "xxx"
+ * StringUtils.repeat("?", ", ", 3) = "?, ?, ?"
+ * </pre>
+ *
+ * @param str the String to repeat, may be null
+ * @param separator the String to inject, may be null
+ * @param repeat number of times to repeat str, negative treated as zero
+ * @return a new String consisting of the original String repeated,
+ * <code>null</code> if null String input
+ */
+ public static String repeat(String str, String separator, int repeat) {
+ if(str == null || separator == null) {
+ return repeat(str, repeat);
+ } else {
+ // given that repeat(String, int) is quite optimized, better to rely on it than try and splice this into it
+ String result = repeat(str + separator, repeat);
+ return removeEnd(result, separator);
+ }
+ }
+
+ /**
* <p>Returns padding using the specified delimiter repeated
* to a given length.</p>
*
Modified: commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java?rev=826955&r1=826954&r2=826955&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java Tue Oct 20 05:18:57 2009
@@ -1153,6 +1153,19 @@
assertEquals(true, StringUtils.containsOnly(str, new char[] {'a'}));
}
+ public void testRepeat_StringStringInt() {
+ assertEquals(null, StringUtils.repeat(null, null, 2));
+ assertEquals(null, StringUtils.repeat(null, "x", 2));
+ assertEquals("", StringUtils.repeat("", null, 2));
+
+ assertEquals("", StringUtils.repeat("ab", "", 0));
+ assertEquals("", StringUtils.repeat("", "", 2));
+
+ assertEquals("xx", StringUtils.repeat("", "x", 3));
+
+ assertEquals("?, ?, ?", StringUtils.repeat("?", ", ", 3));
+ }
+
public void testChop() {
String[][] chopCases = {