String replacement method does not replace characters

I have a sentence that is passed as a string, and I replace the word "and" and I want to replace it with "". And this does not replace the word "and" with a space. Below is an example of my logic. And when I debug this, the logic gets into the sentence. Replace.

String sentence = "Define, Measure, Analyze, Design and Verify" if (sentence.contains("and")){ sentence.replace("and", " "); } 

Something is missing here.

+78
java string replace
04 Oct '12 at 19:45
source share
5 answers

And when I debug this, the logic gets into the sentence. replace.

Yes, and then you discard the return value.

Strings in Java are immutable - when you replace it does not change the contents of an existing row - it returns a new row with the changes. So you want:

 sentence = sentence.replace("and", " "); 

This applies to all methods in String ( substring , toLowerCase , etc.). None of them change the contents of the string.

Note that you really do not need to do this in the state - after all, if the sentence does not contain "and" , this does not harm the replacement:

 String sentence = "Define, Measure, Analyze, Design and Verify"; sentence = sentence.replace("and", " "); 
+165
04 Oct
source share

Strings are immutable, that is, their contents cannot change. When you call replace(this,that) , you get a whole new line. If you want to keep this new copy, you need to assign it to a variable. You can rewrite the old link (a la sentence = sentence.replace(this,that) or a new link, as shown below:

 public class Test{ public static void main(String[] args) { String sentence = "Define, Measure, Analyze, Design and Verify"; String replaced = sentence.replace("and", ""); System.out.println(replaced); } } 

Note that I removed the contains() check, as this is an unnecessary call. If it does not contain it, the substitute will simply not be able to make any replacements. You only need to have a method in it if what you are replacing is different from the actual state you are checking.

+68
04 Oct
source share

You do nothing with the return value of replace . You will need to assign the result of the method, which is the new String :

 sentence = sentence.replace("and", " "); 

A String is immutable in java. Methods such as replace return a new String .

The contains test is not needed: replace will simply be no-op if there are no instances of text to replace.

+9
04 Oct
source share

You must reassign the replacement result, for example:

  sentence = sentence.replace("and", " "); 

Keep in mind that the String class is immutable , which means that all its methods return a new string and never change the original string in place, so the result of calling a method in an instance of String must be assigned to a variable or used immediately for the change to take effect.

+8
04 Oct
source share
 package com.tulu.ds; public class EmailSecurity { public static void main(String[] args) { System.out.println(returnSecuredEmailID("sample717@gmail.com")); } private static String returnSecuredEmailID(String email){ String str=email.substring(1, email.lastIndexOf("@")-1); return email.replaceAll(email.substring(1, email.lastIndexOf("@")-1),replacewith(str.length(),"*")); } private static String replacewith(int length,String replace) { String finalStr=""; for(int i=0;i<length;i++){ finalStr+=replace; } return finalStr; } } 
0
May 05 '18 at 2:21
source share



All Articles