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.
2 things you should note:
String#replace(String)
will do the job.So just use this code:
from javadoc.
You need to use return value of
replaceAll()
method.replaceAll()
does not replace the characters in the current string, it returns a new string with replacement.outputs
By regex i think this is java, the method
replaceAll()
returns a new String with the substrings replaced, so try this:Output:
In javascript:
In other languages, it would be something similar. Remember to enable global matches.
You are probably not assigning it after doing the replacement or replacing the wrong thing. Try :