Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Replaces each substring of this string that matches the literal target sequence

ID: 3646790 • Letter: R

Question

Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab".

@param in The string to be searched and modified.
@param what The sequence of char values to be replaced
@param with The replacement sequence of char values
@return The resulting string

public class StringReplace
{
public static String replace(String in, String what, String with)
{
****** I need help writing the code here **********
}


public static void main(String[] args) {
System.out.println("replace("I went home.", " ", "-") = "" +replace("I went home.", " ", "-") + """);
System.out.println("replace("I watched the ditch.", "tch", "sh") = "" +replace("I watched the ditch.", "tch", "sh") + """);
System.out.println("replace("Not long enough.", "enough...", ".") = "" +replace("Not long enough.", "enough...", ".") + """);
}
}

When you run, it should print:

replace("I went home.", " ", "-") = I-went-home.
replace("I watched the ditch.", "tch", "sh") = I washed the dish
replace("Not long enough.", "enough...", ".") = Not long enough <-- prints out same.

Please help me out. Thank you so much.

Explanation / Answer

* @author Lee Boynton 100 * @author Arthur van Hoff 101 * @author Martin Buchholz 102 * @author Ulf Zibis 103 * @see java.lang.Object#toString() 104 * @see java.lang.StringBuffer 105 * @see java.lang.StringBuilder 106 * @see java.nio.charset.Charset 107 * @since JDK1.0 108 */ 109 110 public final class String 111 implements java.io.Serializable, Comparable, CharSequence 112 { 113 /** The value is used for character storage. */ 114 private final char value[]; 115 116 /** The offset is the first index of the storage that is used. */ 117 private final int offset; 118 119 /** The count is the number of characters in the String. */ 120 private final int count; 121 122 /** Cache the hash code for the string */ 123 private int hash; // Default to 0 124 125 /** use serialVersionUID from JDK 1.0.2 for interoperability */ 126 private static final long serialVersionUID = -6849794470754667710L; 127 128 /** 129 * Class String is special cased within the Serialization Stream Protocol. 130 * 131 * A String instance is written initially into an ObjectOutputStream in the 132 * following format: 133 *
 134 * TC_STRING (utf String) 135 * 
136 * The String is written by method DataOutput.writeUTF. 137 * A new handle is generated to refer to all future references to the 138 * string instance within the stream. 139 */ 140 private static final ObjectStreamField[] serialPersistentFields = 141 new ObjectStreamField[0]; 142 143 /** 144 * Initializes a newly created {@code String} object so that it represents 145 * an empty character sequence. Note that use of this constructor is 146 * unnecessary since Strings are immutable. 147 */ 148 public String() { 149 this.offset = 0; 150 this.count = 0; 151 this.value = new char[0]; 152 } 153