Java String Replace Not Working

Why does String.replace not work?

You did not assign it to test. Strings are immutable.

test = test.replace("KP", "");

You need to assign it back to test.

Java String.replace/replaceAll not working

Strings are immutable in Java. Make sure you re-assign the return value to the same String variable:

str = str.replaceAll("\\[", "");

For the normal replace method, you don't need to escape the bracket:

str = str.replace("[", "");

Java string.replace not working as string.replaceAll

Your real problem

JSON isn't regular at all, you can't just go messing with it like this. You should use a real JSON parser, and whatever gave you this backslashified monstrosity is where the real bug lies. The problem you're facing here can be 'fixed', but your code will remain a fragile mess until you fix this underlying problem!

Your approach to SonarCloud

You shouldn't be using tools like this unless you understand what they do, and given that you ask this question with no idea as to why SonarCloud is even suggesting you replace replaceAll to replace, that sounds like this advice applies to you. Always read the reasoning behind a linting rule.

The problem

.replaceAll replaces each occurrence, but is regular expression based. .replace replaces each occurrence, and isn't - it just replaces the literal string you write. So, all you need to do is .replace("\\", ""). The replaceAll requires 4 backslashes, because that becomes a string with 2 backslashes, which is regexp-ese for '1 backslash'.

String.replaceAll() is not working for some strings

You'll note in the Javadoc for String.replaceAll() that the first argument is a regular expression.

A period (.) has a special meaning there as does a pipe (|) as does a curly brace (}). You need to escape them all, such as:

email = email.replaceAll("gmaii\\.com", "gmail.com");

regex to replace string is not working correctly

Try the following statement:

String strMod = str.replaceAll("\\s\\|\\| '-' \\|\\| ", ",");

The strMod variable will contain the modified value.

The key points are:

  • use escape char in the regular expression because the pipe character has a special meaning in the regexp
  • use the result value of the replaceAll method

String replaceAll not working with string object

String.replaceAll() returns a new string with the specified replacement. You need to do:

myString = myString.replaceAll( "old", "new" );

Why replaceAll($,) is not working although replace($,) works just fine?

$ is a scpecial character in regex (EOL). You have to escape it

pattern.replaceAll("\\$","")

Java String replace not working

String is immutable, which means that the html reference doesn't change, rather the replace method returns a new String object that you have to assign.

html = html.replace(delimiter + entry.getKey()+ delimiter, entry.getValue());

Java String.replace not working

String#replace doesn't support regex, use String#replaceAll:

x = x.replaceAll(".+", "x");


Related Topics



Leave a reply



Submit