Java: removing a string from a StringBuilder

I want to remove String from StringBuilder

Example

String aaa = "sample"; String bbb = "sample2"; String ccc = "sample3"; 

In another part

 StringBuilder ddd = new StringBuilder(); ddd.append(aaa); ddd.append(bbb); ddd.append(ccc); 

I want to check if StringBuilder ddd contains aaa string and removes it

 if (ddd.toString().contains(aaa)) { //Remove String aaa from StringBuilder ddd } 

Is it possible? Or is there any other way to do this?

+6
source share
4 answers

try it

  int i = ddd.indexOf(aaa); if (i != -1) { ddd.delete(i, i + aaa.length()); } 
+10
source

try it

 public void delete(StringBuilder sb, String s) { int start = sb.indexOf(s); if(start < 0) return; sb.delete(start, start + s.length()); } 
+5
source

Create a line from ddd and use replace ().

 ddd.toString().replace(aaa,""); 
+1
source

It can be done:

 ddd.delete(from, to); 
0
source

All Articles