I have a String , String originalString= "This car is my car";
I want to replace "car" with "bike", without using string replace() method.
class StringTest{
public static void main(String[] args){
String originalString = "This car is my car";
String replacedString = replaceMethod(original, "car", "bike");
System.out.println(replacedString);
}
}
static String replaceMethod( String original, String toReplace, String replacedWith){
// enter code here
return "";
}
String.replace. Or use a Matcher and a regular expression (either direct replacement or rebuild with appendReplacement/appendTail). Or use other one-offindexOf/substring/splitand build the result manually. I'd stick withString.replaceunless there is a "good" reason not to.