LANG-1006: Add wrap (with String or char) to StringUtils. This closes PR #21 from github. Thanks to Thiago Andrade.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1592810 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
80f1988264
commit
c3d65461a3
|
@ -22,6 +22,7 @@
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<release version="3.4" date="tba" description="tba">
|
<release version="3.4" date="tba" description="tba">
|
||||||
|
<action issue="LANG-1006" type="update" dev="britter" due-to="Thiago Andrade">Add wrap (with String or char) to StringUtils</action>
|
||||||
<action issue="LANG-1005" type="update" dev="britter" due-to="Michael Osipov">Extend DurationFormatUtils#formatDurationISO default pattern to match #formatDurationHMS</action>
|
<action issue="LANG-1005" type="update" dev="britter" due-to="Michael Osipov">Extend DurationFormatUtils#formatDurationISO default pattern to match #formatDurationHMS</action>
|
||||||
<action issue="LANG-1007" type="update" dev="britter" due-to="Thiago Andrade">Fixing NumberUtils JAVADoc comments for max methods</action>
|
<action issue="LANG-1007" type="update" dev="britter" due-to="Thiago Andrade">Fixing NumberUtils JAVADoc comments for max methods</action>
|
||||||
<action issue="LANG-731" type="update" dev="djones">Better Javadoc for BitField class</action>
|
<action issue="LANG-731" type="update" dev="djones">Better Javadoc for BitField class</action>
|
||||||
|
|
|
@ -588,7 +588,7 @@ public class StringUtils {
|
||||||
str = stripStart(str, stripChars);
|
str = stripStart(str, stripChars);
|
||||||
return stripEnd(str, stripChars);
|
return stripEnd(str, stripChars);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Strips any of a set of characters from the start of a String.</p>
|
* <p>Strips any of a set of characters from the start of a String.</p>
|
||||||
*
|
*
|
||||||
|
@ -7636,4 +7636,73 @@ public class StringUtils {
|
||||||
return new String(bytes, charset != null ? charset : Charset.defaultCharset());
|
return new String(bytes, charset != null ? charset : Charset.defaultCharset());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* Wraps a String with a char.
|
||||||
|
* <p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* A {@code null} input String returns {@code null}.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* StringUtils.wrap(null, *) = null
|
||||||
|
* StringUtils.wrap("", *) = ""
|
||||||
|
* StringUtils.wrap("ab", '\0') = "ab"
|
||||||
|
* StringUtils.wrap("ab", 'x') = "xabx"
|
||||||
|
* StringUtils.wrap("ab", '\'') = "'ab'"
|
||||||
|
* StringUtils.wrap("\"ab\"", '\"') = "\"ab\""
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @param str
|
||||||
|
* the String to be wrapper, may be null
|
||||||
|
* @param wrapWith
|
||||||
|
* the char that will wrap str
|
||||||
|
* @return wrapped String, {@code null} if null String input
|
||||||
|
* @since 3.4
|
||||||
|
*/
|
||||||
|
public static String wrap(String str, char wrapWith) {
|
||||||
|
|
||||||
|
if (isEmpty(str) || wrapWith == '\0')
|
||||||
|
return str;
|
||||||
|
|
||||||
|
return wrapWith + str + wrapWith;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* Wrapps a String with another String.
|
||||||
|
* <p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* A {@code null} input String returns {@code null}.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* StringUtils.wrap(null, *) = null
|
||||||
|
* StringUtils.wrap("", *) = ""
|
||||||
|
* StringUtils.wrap("ab", null) = "ab"
|
||||||
|
* StringUtils.wrap("ab", "x") = "xabx"
|
||||||
|
* StringUtils.wrap("ab", "\"") = "\"ab\""
|
||||||
|
* StringUtils.wrap("\"ab\"", "\"") = "\"\"ab\"\""
|
||||||
|
* StringUtils.wrap("ab", "'") = "'ab'"
|
||||||
|
* StringUtils.wrap("'abcd'", "'") = "''abcd''"
|
||||||
|
* StringUtils.wrap("\"abcd\"", "'") = "'\"abcd\"'"
|
||||||
|
* StringUtils.wrap("'abcd'", "\"") = "\"'abcd'\""
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @param str
|
||||||
|
* the String to be wrapper, may be null
|
||||||
|
* @param wrapWith
|
||||||
|
* the String that will wrap str
|
||||||
|
* @return wrapped String, {@code null} if null String input
|
||||||
|
* @since 3.4
|
||||||
|
*/
|
||||||
|
public static String wrap(String str, String wrapWith) {
|
||||||
|
|
||||||
|
if (isEmpty(str) || isEmpty(wrapWith))
|
||||||
|
return str;
|
||||||
|
|
||||||
|
return wrapWith.concat(str).concat(wrapWith);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2454,4 +2454,41 @@ public class StringUtilsTest {
|
||||||
expectedBytes = expectedString.getBytes(encoding);
|
expectedBytes = expectedString.getBytes(encoding);
|
||||||
assertEquals(expectedString, StringUtils.toEncodedString(expectedBytes, Charset.forName(encoding)));
|
assertEquals(expectedString, StringUtils.toEncodedString(expectedBytes, Charset.forName(encoding)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWrap_StringChar() {
|
||||||
|
assertNull(StringUtils.wrap(null, null));
|
||||||
|
assertNull(StringUtils.wrap(null, '\0'));
|
||||||
|
assertNull(StringUtils.wrap(null, '1'));
|
||||||
|
|
||||||
|
assertEquals(null, StringUtils.wrap(null, null));
|
||||||
|
assertEquals("", StringUtils.wrap("", '\0'));
|
||||||
|
assertEquals("xabx", StringUtils.wrap("ab", 'x'));
|
||||||
|
assertEquals("\"ab\"", StringUtils.wrap("ab", '\"'));
|
||||||
|
assertEquals("\"\"ab\"\"", StringUtils.wrap("\"ab\"", '\"'));
|
||||||
|
assertEquals("'ab'", StringUtils.wrap("ab", '\''));
|
||||||
|
assertEquals("''abcd''", StringUtils.wrap("'abcd'", '\''));
|
||||||
|
assertEquals("'\"abcd\"'", StringUtils.wrap("\"abcd\"", '\''));
|
||||||
|
assertEquals("\"'abcd'\"", StringUtils.wrap("'abcd'", '\"'));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWrap_StringString() {
|
||||||
|
assertNull(StringUtils.wrap(null, null));
|
||||||
|
assertNull(StringUtils.wrap(null, ""));
|
||||||
|
assertNull(StringUtils.wrap(null, "1"));
|
||||||
|
|
||||||
|
assertEquals(null, StringUtils.wrap(null, null));
|
||||||
|
assertEquals("", StringUtils.wrap("", ""));
|
||||||
|
assertEquals("ab", StringUtils.wrap("ab", null));
|
||||||
|
assertEquals("xabx", StringUtils.wrap("ab", "x"));
|
||||||
|
assertEquals("\"ab\"", StringUtils.wrap("ab", "\""));
|
||||||
|
assertEquals("\"\"ab\"\"", StringUtils.wrap("\"ab\"", "\""));
|
||||||
|
assertEquals("'ab'", StringUtils.wrap("ab", "'"));
|
||||||
|
assertEquals("''abcd''", StringUtils.wrap("'abcd'", "'"));
|
||||||
|
assertEquals("'\"abcd\"'", StringUtils.wrap("\"abcd\"", "'"));
|
||||||
|
assertEquals("\"'abcd'\"", StringUtils.wrap("'abcd'", "\""));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue