Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android replace multiple characters from a string

Tags:

java

android

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

like image 392
Tuffy G Avatar asked Nov 27 '22 11:11

Tuffy G


2 Answers

You can nest them up too:

 output=String.valueOf(profile.get("text")).replace(s, w).replace(q, w);
like image 109
noob Avatar answered Dec 08 '22 17:12

noob


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.

like image 21
hmjd Avatar answered Dec 08 '22 18:12

hmjd