JDK-4840253 : Add another overloaded replace() method to java.lang.String
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.4.1
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2003-03-31
  • Updated: 2004-11-04
  • Resolved: 2004-11-04
Related Reports
Duplicate :  
Relates :  
Relates :  
Description
Name: nt126004			Date: 03/31/2003


A DESCRIPTION OF THE REQUEST :
Overloaded the current String replace(char oldChar, char newChar) method in java.lang.String with:

String replace(String oldSubString, String newString)
          Returns a new string resulting from replacing all occurrences of oldSubString in this string with newString.

I understand that there has been a replaceAll() method in J2SE 1.4, but many times, that would
be an overkill for simple string replacement operations. Using Regex adds complexity to the
program and so increases its memory footprint during execution. Adding another overloaded
String.replace(String, String), to go in hand with String.replace(char, char), would simplify
and expedite coding in many simple string manipulation/replacement operations.

A review for Bug Id: 4834877 has gone through. That submission is for the StringBuffer.

JUSTIFICATION :
A lot of string manipulation operations involves string replacement, rather than mere character replacement. Because the situation is so frequently encountered, it's a nuisance to write that piece of codes everytime. Including this overload method will save developers time and codings.


CUSTOMER SUBMITTED WORKAROUND :
    /**
     *  String replacement
     *
     */
    String replace(String text, final String pattern, final String replace) {
        int startIndex = 0;
        int foundIndex;
        StringBuffer result = new StringBuffer();

        // Look for a pattern to replace
        while ((foundIndex = text.indexOf(pattern, startIndex)) >= 0) {
            result.append(text.substring(startIndex, foundIndex));
            result.append(replace);
            startIndex = foundIndex + pattern.length();
        }
        result.append(text.substring(startIndex));
        return result.toString();
    }
(Review ID: 182669) 
======================================================================
###@###.### 11/4/04 19:23 GMT

Comments
EVALUATION The following method was added during Tiger development: public String replace(CharSequence target, CharSequence replacement); Since String implements CharSequence, the requested functionality is present. Originally, the fixes to bugs 4808962 and 4803171 added the replace method with String parameters in b10 (well in advance of Tiger-beta1). We later determined that these new methods should have been declared with CharSequence parameters in bug 5019917. This bug was fixed in b48 (in time for Tiger-beta2). Closing as duplicate of 4808962. ###@###.### 11/4/04 19:23 GMT
04-11-2004