Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace a substring of a string [duplicate]

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.

like image 349
MichalB Avatar asked May 22 '13 22:05

MichalB


People also ask

How do I replace a substring with another string?

Algorithm to Replace a substring in a stringInput the full string (s1). Input the substring from the full string (s2). Input the string to be replaced with the substring (s3). Find the substring from the full string and replace the new substring with the old substring (Find s2 from s1 and replace s1 by s3).

How do you replace one part of a string?

One of the simplest and straightforward methods of replacing a substring is using the replace, replaceAll or replaceFirst of a String class.

How do you replace a substring in C++?

Replace substring with another substring C++ It replaces the portion of the string that begins at character pos and spans len characters. The structure of the replace function is like below: string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen);

How do you replace a substring in a string in Java without using replace () method?

To replace a character in a String, without using the replace() method, try the below logic. Let's say the following is our string. int pos = 7; char rep = 'p'; String res = str. substring(0, pos) + rep + str.


2 Answers

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 
like image 59
Alper Avatar answered Sep 19 '22 15:09

Alper


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"); 
like image 29
anubhava Avatar answered Sep 17 '22 15:09

anubhava