I know that this may be an amateur question but for some reason I can't remember how to do this. I have 2 strings.
String s ="[";
String q ="]";
if my text contains any of these i want to replace it with w
which is:
String w = "";
I have tried the following:
output=String.valueOf(profile.get("text")).replace(s&&q, w);
from what i understand if of S([) and any of Q(]) are in text they will be replaced with w. my problem is getting the 2. if i only try and replace one then it will work. otherwise it wont.
any help would be appreciated
You can nest them up too:
output=String.valueOf(profile.get("text")).replace(s, w).replace(q, w);
I think this is what you mean:
String s = "abc[def]";
String w = "hello";
System.out.println(s.replaceAll("\\[|\\]", w));
Outputs abchellodefhello
.
String.replaceAll()
accepts a regular expression as its first argument, which would provide the flexibility required.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With