From 357851bdb1830b1275221b28aee676f9860e1d8b Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Thu, 31 Jul 2003 20:38:26 +0000 Subject: [PATCH] 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 --- .../org/apache/commons/lang/CharSetUtils.java | 4 +- .../org/apache/commons/lang/StringUtils.java | 105 +++++++++++++++++- .../apache/commons/lang/StringUtilsTest.java | 43 ++++++- 3 files changed, 149 insertions(+), 3 deletions(-) diff --git a/src/java/org/apache/commons/lang/CharSetUtils.java b/src/java/org/apache/commons/lang/CharSetUtils.java index 7d5c68b0c..6bdb676cf 100644 --- a/src/java/org/apache/commons/lang/CharSetUtils.java +++ b/src/java/org/apache/commons/lang/CharSetUtils.java @@ -63,7 +63,7 @@ package org.apache.commons.lang; * @author Henri Yandell * @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 with or repl * is null * @throws ArrayIndexOutOfBoundsException if with 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) { diff --git a/src/java/org/apache/commons/lang/StringUtils.java b/src/java/org/apache/commons/lang/StringUtils.java index a4f4779ed..cf5c5e162 100644 --- a/src/java/org/apache/commons/lang/StringUtils.java +++ b/src/java/org/apache/commons/lang/StringUtils.java @@ -146,7 +146,7 @@ import java.util.List; * @author Gary Gregory * @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 + //----------------------------------------------------------------------- + + /** + *

Replaces all occurrances of a character in a String with another. + * This is a null-safe version of {@link String#replace(char, char)}.

+ * + *

A null string input returns null. + * An empty ("") string input returns an empty string.

+ * + *
+     * StringUtils.replaceChars(null, *, *)        = null
+     * StringUtils.replaceChars("", *, *)          = ""
+     * StringUtils.replaceChars("abcba", 'b', 'y') = "aycya"
+     * StringUtils.replaceChars("abcba", 'z', 'y') = "abcba"
+     * 
+ * + * @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, null if null string input + */ + public static String replaceChars(String str, char searchChar, char replaceChar) { + if (str == null) { + return null; + } + return str.replace(searchChar, replaceChar); + } + + /** + *

Replaces multiple characters in a String in one go. + * This method can also be used to delete characters.

+ * + *

For example:
+ * replaceChars("hello", "ho", "jy") = jelly.

+ * + *

A null string input returns null. + * An empty ("") string input returns an empty string. + * A null or empty set of search characters returns the input string.

+ * + *

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.

+ * + *
+     * 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"
+     * 
+ * + * @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, null 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 + //----------------------------------------------------------------------- /** *

Overlays part of a String with another String.

diff --git a/src/test/org/apache/commons/lang/StringUtilsTest.java b/src/test/org/apache/commons/lang/StringUtilsTest.java index bbc910b0b..7bb36e519 100644 --- a/src/test/org/apache/commons/lang/StringUtilsTest.java +++ b/src/test/org/apache/commons/lang/StringUtilsTest.java @@ -74,7 +74,7 @@ import junit.textui.TestRunner; * @author Holger Krauth * @author Henning P. Schmiedehausen * @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) );