Add replaceChars() to StringUtils
Deprecate translate() on CharSetUtils git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137537 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
5596790adb
commit
357851bdb1
|
@ -63,7 +63,7 @@ package org.apache.commons.lang;
|
|||
* @author <a href="bayard@generationjava.com">Henri Yandell</a>
|
||||
* @author Stephen Colebourne
|
||||
* @since 1.0
|
||||
* @version $Id: CharSetUtils.java,v 1.16 2003/07/30 22:17:00 scolebourne Exp $
|
||||
* @version $Id: CharSetUtils.java,v 1.17 2003/07/31 20:38:26 scolebourne Exp $
|
||||
*/
|
||||
public class CharSetUtils {
|
||||
|
||||
|
@ -345,6 +345,8 @@ public class CharSetUtils {
|
|||
* @throws NullPointerException if <code>with</code> or <code>repl</code>
|
||||
* is <code>null</code>
|
||||
* @throws ArrayIndexOutOfBoundsException if <code>with</code> is empty ("")
|
||||
* @deprecated Use {@link StringUtils#replaceChars(String, String, String)}.
|
||||
* Method will be removed in Commons Lang 3.0.
|
||||
*/
|
||||
public static String translate(String str, String searchChars, String replaceChars) {
|
||||
if (str == null || str.length() == 0) {
|
||||
|
|
|
@ -146,7 +146,7 @@ import java.util.List;
|
|||
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
|
||||
* @author Phil Steitz
|
||||
* @since 1.0
|
||||
* @version $Id: StringUtils.java,v 1.82 2003/07/30 22:17:49 scolebourne Exp $
|
||||
* @version $Id: StringUtils.java,v 1.83 2003/07/31 20:38:26 scolebourne Exp $
|
||||
*/
|
||||
public class StringUtils {
|
||||
// Performance testing notes (JDK 1.4, Jul03, scolebourne)
|
||||
|
@ -2261,6 +2261,109 @@ public class StringUtils {
|
|||
buf.append(text.substring(start));
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
// Replace, character based
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* <p>Replaces all occurrances of a character in a String with another.
|
||||
* This is a null-safe version of {@link String#replace(char, char)}.</p>
|
||||
*
|
||||
* <p>A <code>null</code> string input returns <code>null</code>.
|
||||
* An empty ("") string input returns an empty string.</p>
|
||||
*
|
||||
* <pre>
|
||||
* StringUtils.replaceChars(null, *, *) = null
|
||||
* StringUtils.replaceChars("", *, *) = ""
|
||||
* StringUtils.replaceChars("abcba", 'b', 'y') = "aycya"
|
||||
* StringUtils.replaceChars("abcba", 'z', 'y') = "abcba"
|
||||
* </pre>
|
||||
*
|
||||
* @param str String to replace characters in, may be null
|
||||
* @param searchChar the character to search for, may be null
|
||||
* @param replaceChar the character to replace, may be null
|
||||
* @return modified String, <code>null</code> if null string input
|
||||
*/
|
||||
public static String replaceChars(String str, char searchChar, char replaceChar) {
|
||||
if (str == null) {
|
||||
return null;
|
||||
}
|
||||
return str.replace(searchChar, replaceChar);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Replaces multiple characters in a String in one go.
|
||||
* This method can also be used to delete characters.</p>
|
||||
*
|
||||
* <p>For example:<br />
|
||||
* <code>replaceChars("hello", "ho", "jy") = jelly</code>.</p>
|
||||
*
|
||||
* <p>A <code>null</code> string input returns <code>null</code>.
|
||||
* An empty ("") string input returns an empty string.
|
||||
* A null or empty set of search characters returns the input string.</p>
|
||||
*
|
||||
* <p>The length of the search characters should normally equal the length
|
||||
* of the replace characters.
|
||||
* If the search characters is longer, then the extra search characters
|
||||
* are deleted.
|
||||
* If the search characters is shorter, then the extra replace characters
|
||||
* are ignored.</p>
|
||||
*
|
||||
* <pre>
|
||||
* StringUtils.replaceChars(null, *, *) = null
|
||||
* StringUtils.replaceChars("", *, *) = ""
|
||||
* StringUtils.replaceChars("abc", null, *) = "abc"
|
||||
* StringUtils.replaceChars("abc", "", *) = "abc"
|
||||
* StringUtils.replaceChars("abc", "b", null) = "ac"
|
||||
* StringUtils.replaceChars("abc", "b", "") = "ac"
|
||||
* StringUtils.replaceChars("abcba", "bc", "yz") = "ayzya"
|
||||
* StringUtils.replaceChars("abcba", "bc", "y") = "ayya"
|
||||
* StringUtils.replaceChars("abcba", "bc", "yzx") = "ayzya"
|
||||
* </pre>
|
||||
*
|
||||
* @param str String to replace characters in, may be null
|
||||
* @param searchChars a set of characters to search for, may be null
|
||||
* @param replaceChars a set of characters to replace, may be null
|
||||
* @return modified String, <code>null</code> if null string input
|
||||
*/
|
||||
public static String replaceChars(String str, String searchChars, String replaceChars) {
|
||||
if (str == null || str.length() == 0 || searchChars == null || searchChars.length()== 0) {
|
||||
return str;
|
||||
}
|
||||
char[] chars = str.toCharArray();
|
||||
int len = chars.length;
|
||||
boolean modified = false;
|
||||
for (int i = 0, isize = searchChars.length(); i < isize; i++) {
|
||||
char searchChar = searchChars.charAt(i);
|
||||
if (replaceChars == null || i >= replaceChars.length()) {
|
||||
// delete
|
||||
int pos = 0;
|
||||
for (int j = 0; j < len; j++) {
|
||||
if (chars[j] != searchChar) {
|
||||
chars[pos++] = chars[j];
|
||||
} else {
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
len = pos;
|
||||
} else {
|
||||
// replace
|
||||
for (int j = 0; j < len; j++) {
|
||||
if (chars[j] == searchChar) {
|
||||
chars[j] = replaceChars.charAt(i);
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (modified == false) {
|
||||
return str;
|
||||
}
|
||||
return new String(chars, 0, len);
|
||||
}
|
||||
|
||||
// Overlay
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* <p>Overlays part of a String with another String.</p>
|
||||
|
|
|
@ -74,7 +74,7 @@ import junit.textui.TestRunner;
|
|||
* @author Holger Krauth
|
||||
* @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
|
||||
* @author Phil Steitz
|
||||
* @version $Id: StringUtilsTest.java,v 1.40 2003/07/30 22:21:39 scolebourne Exp $
|
||||
* @version $Id: StringUtilsTest.java,v 1.41 2003/07/31 20:38:26 scolebourne Exp $
|
||||
*/
|
||||
public class StringUtilsTest extends TestCase {
|
||||
|
||||
|
@ -439,6 +439,47 @@ public class StringUtilsTest extends TestCase {
|
|||
assertEquals("foofoo", StringUtils.replaceOnce("foofoofoo", "foo", ""));
|
||||
}
|
||||
|
||||
public void testReplaceChars_StringCharChar() {
|
||||
assertEquals(null, StringUtils.replaceChars(null, 'b', 'z'));
|
||||
assertEquals("", StringUtils.replaceChars("", 'b', 'z'));
|
||||
assertEquals("azcza", StringUtils.replaceChars("abcba", 'b', 'z'));
|
||||
assertEquals("abcba", StringUtils.replaceChars("abcba", 'x', 'z'));
|
||||
}
|
||||
|
||||
public void testReplaceChars_StringStringString() {
|
||||
assertEquals("jelly", StringUtils.replaceChars("hello", "ho", "jy"));
|
||||
|
||||
assertEquals(null, StringUtils.replaceChars(null, null, null));
|
||||
assertEquals(null, StringUtils.replaceChars(null, "", null));
|
||||
assertEquals(null, StringUtils.replaceChars(null, "a", null));
|
||||
assertEquals(null, StringUtils.replaceChars(null, null, ""));
|
||||
assertEquals(null, StringUtils.replaceChars(null, null, "x"));
|
||||
|
||||
assertEquals("", StringUtils.replaceChars("", null, null));
|
||||
assertEquals("", StringUtils.replaceChars("", "", null));
|
||||
assertEquals("", StringUtils.replaceChars("", "a", null));
|
||||
assertEquals("", StringUtils.replaceChars("", null, ""));
|
||||
assertEquals("", StringUtils.replaceChars("", null, "x"));
|
||||
|
||||
assertEquals("abc", StringUtils.replaceChars("abc", null, null));
|
||||
assertEquals("abc", StringUtils.replaceChars("abc", null, ""));
|
||||
assertEquals("abc", StringUtils.replaceChars("abc", null, "x"));
|
||||
|
||||
assertEquals("abc", StringUtils.replaceChars("abc", "", null));
|
||||
assertEquals("abc", StringUtils.replaceChars("abc", "", ""));
|
||||
assertEquals("abc", StringUtils.replaceChars("abc", "", "x"));
|
||||
|
||||
assertEquals("ac", StringUtils.replaceChars("abc", "b", null));
|
||||
assertEquals("ac", StringUtils.replaceChars("abc", "b", ""));
|
||||
assertEquals("axc", StringUtils.replaceChars("abc", "b", "x"));
|
||||
|
||||
assertEquals("ayzya", StringUtils.replaceChars("abcba", "bc", "yz"));
|
||||
assertEquals("ayya", StringUtils.replaceChars("abcba", "bc", "y"));
|
||||
assertEquals("ayzya", StringUtils.replaceChars("abcba", "bc", "yzx"));
|
||||
|
||||
assertSame("abcba", StringUtils.replaceChars("abcba", "z", "w"));
|
||||
}
|
||||
|
||||
public void testOverlayString() {
|
||||
assertEquals("overlayString(String, String, int, int) failed",
|
||||
"foo foor baz", StringUtils.overlayString(SENTENCE, FOO, 4, 6) );
|
||||
|
|
Loading…
Reference in New Issue