Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Remove String from StringBuilder [duplicate]

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 String aaa and remove it

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

Is that possible? Or is there any other way to do like that?

like image 887
user2341387 Avatar asked Jan 28 '14 14:01

user2341387


2 Answers

Create a string from ddd and use replace().

ddd.toString().replace(aaa,"");
like image 105
Fco P. Avatar answered Sep 28 '22 01:09

Fco P.


try this

    int i = ddd.indexOf(aaa);
    if (i != -1) {
        ddd.delete(i, i + aaa.length());
    }
like image 44
Evgeniy Dorofeev Avatar answered Sep 28 '22 03:09

Evgeniy Dorofeev