Java String.Replace/Replaceall Not Working

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

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("[", "");

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

String.replaceAll() doesn't work on some files and strings

The parentheses in your 'oldString' are special characters within a regular expression used for grouping. Therefore it doesn't match.

You can test by pasting your text and regex here or other online tools.

You should actually escape your brackets by add a backslash in front of it. => Potenza\(CV\)=200

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

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

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

replaceAll does not replace string

Change

query.replaceAll("REPLACEME", symbols.toString());

to:

query = query.replaceAll("REPLACEME", symbols.toString());

Strings in Java are designed to be immutable.

That is why replaceAll() can't replace the characters in the current string, so it must return a new string with the characters replaced.


Also if you want to simply replace literals and don't need regex syntax support use replace instead of replaceAll (regex syntax support is only difference between these two methods). It is safer in case you would want to replace literals which can contain regex metacharacters like *, +, [, ] and others.

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

String.replaceAll(...) of Java not working for \\ and \

You have to use :

String t2 = t1.replaceAll("\\\\", "\\\\\\\\");

or (without pattern) :

String t2 = t1.replace("\\", "\\\\");

Each "\" has to be preceeded by an other "\". But it's also true for the preceeding "\" so you have to write four backslashes each time you want one in regex.



Related Topics



Leave a reply



Submit