Your solution works but with a slight modification like below. Remove the (?i)
part if you dont want to take case sensitivity into account.
public class StringReplacer {
public static void main(String[] args) {
String str1 = "New York";
String str2 = "New Jersy";
for(String s : str1.split("(?i)[" + str2 +"]")){
if(s.trim().length() > 0){
str1 = str1.replace(s, "+");
}
}
System.out.println(str1); // Prints New Y+r+
}
}
solved match two string and replace non-match char to + in java [closed]