How to replace a substring of a string [duplicate]

2020-01-29 05:19发布

Assuming I have a String string like this:

"abcd=0; efgh=1"

and I want to replace "abcd" by "dddd". I have tried to do such thing:

string.replaceAll("abcd","dddd");

It does not work. Any suggestions?

EDIT: To be more specific, I am working in Java and I am trying to parse the HTML document, concretely the content between <script> tags. I have already found a way how to parse this content into a string:

 if(tag instanceof ScriptTag){
        if(((ScriptTag) tag).getStringText().contains("DataVideo")){
            String tagText = ((ScriptTag)tag).getStringText();
      }
}

Now I have to find a way how to replace one substring by another one.

7条回答
男人必须洒脱
2楼-- · 2020-01-29 06:02

You need to create the variable to assign the new value to, like this:

String str = string.replaceAll("abcd","dddd");
查看更多
登录 后发表回答