Index: modules/luni/src/test/java/tests/api/java/lang/StringTest.java =================================================================== --- modules/luni/src/test/java/tests/api/java/lang/StringTest.java (revision 428300) +++ modules/luni/src/test/java/tests/api/java/lang/StringTest.java (working copy) @@ -643,6 +643,18 @@ } /** + * @tests java.lang.String#replace(CharSequence, CharSequence) + */ + public void test_replaceLjava_langCharSequence() { + assertEquals("Failed replace", "aaccdd", "aabbdd".replace( + new StringBuffer("bb"), "cc")); + assertEquals("Failed replace by bigger seq", "cccbccc", "aba".replace( + "a", "ccc")); + assertEquals("Failed replace by smaller seq", "bba", + "aaaaa".replace(new StringBuilder("aa"), "b")); + } + + /** * @tests java.lang.String#startsWith(java.lang.String) */ public void test_startsWithLjava_lang_String() { Index: modules/luni/src/main/java/java/lang/String.java =================================================================== --- modules/luni/src/main/java/java/lang/String.java (revision 428300) +++ modules/luni/src/main/java/java/lang/String.java (working copy) @@ -1280,6 +1280,48 @@ } /** + * Copies this String replacing occurrences of the specified + * target sequence with another sequence. + * The string is processed from the beginning to the end. + * + * @param target + * the sequence to replace + * @param replacement + * the replacement sequence + * @return the resulting String + * + * @throws NullPointerException if either of the arguments + * is null + */ + public String replace(CharSequence target, CharSequence replacement) { + if (target == null) { + throw new NullPointerException("target should not be null"); + } + if (replacement == null) { + throw new NullPointerException("replacement should not be null"); + } + String ts = target.toString(); + int index = indexOf(ts, 0); + + if (index == -1) + return this; + + String rs = replacement.toString(); + StringBuilder buffer = new StringBuilder(count); + int tl = target.length(); + int tail = 0; + do { + buffer.append(value, offset + tail, index - tail); + buffer.append(rs); + tail = index + tl; + } while ((index = indexOf(ts, tail)) != -1); + //append trailing chars + buffer.append(value, offset + tail, count - tail); + + return buffer.toString(); + } + + /** * Compares the specified string to this String to determine if the * specified string is a prefix. *