How to replace a substring of a string

JavaStringSubstring

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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.

> - String objects are immutable, their values cannot be changed after they are created. > - You may use replace() instead of replaceAll() if you don't need regex.

    String str = "abcd=0; efgh=1";
    String replacedStr = str.replaceAll("abcd", "dddd");

    System.out.println(str);
    System.out.println(replacedStr);

outputs

abcd=0; efgh=1
dddd=0; efgh=1

Solution 2 - Java

2 things you should note:

  1. Strings in Java are immutable to so you need to store return value of thereplace method call in another String.
  2. You don't really need a regex here, just a simple call to String#replace(String) will do the job.

So just use this code:

String replaced = string.replace("abcd", "dddd");

Solution 3 - Java

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

String str = string.replaceAll("abcd","dddd");

Solution 4 - Java

By regex i think this is java, the method replaceAll() returns a new String with the substrings replaced, so try this:

String teste = "abcd=0; efgh=1";

String teste2 = teste.replaceAll("abcd", "dddd");

System.out.println(teste2);

Output:

dddd=0; efgh=1

Solution 5 - Java

> Note that backslashes (\) and dollar signs ($) in the replacement > string may cause the results to be different than if it were being > treated as a literal replacement string; see > Matcher.replaceAll. > Use > Matcher.quoteReplacement(java.lang.String) > to suppress the special meaning of these characters, if desired.

from javadoc.

Solution 6 - Java

You are probably not assigning it after doing the replacement or replacing the wrong thing. Try :

String haystack = "abcd=0; efgh=1";
String result = haystack.replaceAll("abcd","dddd");

Solution 7 - Java

In javascript:

var str = "abcdaaaaaabcdaabbccddabcd";
document.write(str.replace(/(abcd)/g,"----"));
//example output: ----aaaaa----aabbccdd----

In other languages, it would be something similar. Remember to enable global matches.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichalBView Question on Stackoverflow
Solution 1 - JavaAlperView Answer on Stackoverflow
Solution 2 - JavaanubhavaView Answer on Stackoverflow
Solution 3 - JavaEddyRView Answer on Stackoverflow
Solution 4 - JavaRicardo CacheiraView Answer on Stackoverflow
Solution 5 - JavaAmr LotfyView Answer on Stackoverflow
Solution 6 - JavaBwireView Answer on Stackoverflow
Solution 7 - JavaIsaacView Answer on Stackoverflow