62

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.

Barranka
  • 20,547
  • 13
  • 65
  • 83
MichalB
  • 3,281
  • 6
  • 32
  • 46
  • 3
    Can you be more specific than "it does not work?" First of all, what language is this? Do you get an error? Does something unexpected happen? What are you trying to accomplish in general? – Explosion Pills May 22 '13 at 22:05

6 Answers6

86

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
Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
Alper
  • 12,860
  • 2
  • 31
  • 41
  • Note that replaceAll expects a regular expression. Replace blank parens, "()", will produce a regex error, until you do proper escaping: str.replaceAll("\\\(\\\)", ""); – HoldOffHunger Sep 19 '16 at 00:13
16

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");
anubhava
  • 761,203
  • 64
  • 569
  • 643
13

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

String str = string.replaceAll("abcd","dddd");
Tom
  • 16,842
  • 17
  • 45
  • 54
EddyR
  • 159
  • 1
  • 1
  • 7
9

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
4

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.

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
Amr Lotfy
  • 2,937
  • 5
  • 36
  • 56
0

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");
Bwire
  • 1,181
  • 1
  • 14
  • 25