简体   繁体   中英

How do I replace everything except for the parameter String (Word)

public String plusOut(String str, String word){

String bob = str.replaceAll(^word,"+");

return bob;

}

Sample Input and Output:

I want to replace everything in String(str) that's not String(word) with a +

plusOut("1234xy5678", "xy")     == "++++xy++++"
plusOut("ghlnds4pl4qwqd4", "4") == "++++++4++4++++4"

^word<---how would I make the method replace everything except for word

I want to replace my String(str) with a "+" except the String(word). How would I go about doing this using replaceAll method.

Your replaceAll method is almost there.

You need to change it to something like:

String bob = str.replaceAll("[^" + word + "]", "+");

You can see the two outputs requested here and here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM