如何替换字符串的子串[重复]

时间:2021-10-05 19:11:23

This question already has an answer here:

这个问题在这里已有答案:

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:

我想用“dddd”替换“abcd”。我试过做这样的事情:

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:

编辑:更具体地说,我在Java工作,我正在尝试解析HTML文档,具体是

 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 个解决方案

#1


63  

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.

您需要使用replaceAll()方法的返回值。 replaceAll()不替换当前字符串中的字符,它返回带有替换的新字符串。

  • 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.
  • 如果不需要正则表达式,可以使用replace()而不是replaceAll()。
    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

#2


10  

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. Java中的字符串是不可变的,因此您需要将whereplace方法调用的返回值存储在另一个String中。
  3. You don't really need a regex here, just a simple call to String#replace(String) will do the job.
  4. 你真的不需要这里的正则表达式,只需简单调用String#replace(String)就可以了。

So just use this code:

所以只需使用此代码:

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

#3


10  

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

您需要创建变量以将新值分配给,如下所示:

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

#4


4  

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

通过正则表达式我认为这是java,方法replaceAll()返回一个替换了子字符串的新String,所以试试这个:

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

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

System.out.println(teste2);

Output:

输出:

dddd=0; efgh=1

#5


3  

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 java.util.regex.Matcher.quoteReplacement to suppress the special meaning of these characters, if desired.

请注意,替换字符串中的反斜杠(\)和美元符号($)可能会导致结果与将其视为文字替换字符串时的结果不同;见Matcher.replaceAll。如果需要,请使用java.util.regex.Matcher.quoteReplacement来抑制这些字符的特殊含义。

from javadoc.

来自javadoc。

#6


0  

In javascript:

在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.

在其他语言中,它将是类似的东西。请记住启用全局匹配。

#7


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");

#1


63  

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.

您需要使用replaceAll()方法的返回值。 replaceAll()不替换当前字符串中的字符,它返回带有替换的新字符串。

  • 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.
  • 如果不需要正则表达式,可以使用replace()而不是replaceAll()。
    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

#2


10  

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. Java中的字符串是不可变的,因此您需要将whereplace方法调用的返回值存储在另一个String中。
  3. You don't really need a regex here, just a simple call to String#replace(String) will do the job.
  4. 你真的不需要这里的正则表达式,只需简单调用String#replace(String)就可以了。

So just use this code:

所以只需使用此代码:

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

#3


10  

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

您需要创建变量以将新值分配给,如下所示:

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

#4


4  

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

通过正则表达式我认为这是java,方法replaceAll()返回一个替换了子字符串的新String,所以试试这个:

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

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

System.out.println(teste2);

Output:

输出:

dddd=0; efgh=1

#5


3  

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 java.util.regex.Matcher.quoteReplacement to suppress the special meaning of these characters, if desired.

请注意,替换字符串中的反斜杠(\)和美元符号($)可能会导致结果与将其视为文字替换字符串时的结果不同;见Matcher.replaceAll。如果需要,请使用java.util.regex.Matcher.quoteReplacement来抑制这些字符的特殊含义。

from javadoc.

来自javadoc。

#6


0  

In javascript:

在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.

在其他语言中,它将是类似的东西。请记住启用全局匹配。

#7


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");