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 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 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.Replace() is not working as expected to replace weird chars in strings in C#
Change this:
DisplayName.Replace("'", "-").Replace("\"", "-");
To this:
DisplayName = DisplayName.Replace("'", "-").Replace("\"", "-");
Java String.replace not working
String#replace
doesn't support regex, use String#replaceAll
:
x = x.replaceAll(".+", "x");
python: why does replace not work?
String.replace(substr)
does not happen in place, change it to:
string = string.replace("http://","")
String.replace() not replacing all occurrences
You need to replace the trailing comma as well (if one exists, which it won't if last in the list):
str = str.replaceAll("\\b382,?", "");
Note \b
word boundary to prevent matching "-,1382,-"
.
The above will convert:
382,111,382,1382,222,382
to:
111,1382,222
String replace doesn't appear to be working
Strings in Python are immutable. That means that a given string object will never have its value changed after it has been created. This is why an element assignment like some_str[4] = "x"
will raise an exception.
For a similar reason, none of the methods provided by the str
class can mutate the string. So, the str.replace
method does not work the way I think you expect it to. Rather than modifying the string in place, it returns a new string with the requested replacements.
Try:
encrypted_str = encrypted_str.replace(encrypted_str[j], dec_str2[k], 2)
If you're going to be making many such replacements, it may make sense to turn your string into a list of characters, make the modifications one by one, then use str.join
to turn the list back into a string again when you're done.
Android String.replace not working
String text;
text= "2:15";
if(text.contains(":"))
{
replace(":","."); // Will not cause anything as String is immutable.
}
Log.i("Tag",text);
Change to
String text;
text="2:15";
if(text.contains(":")){
text = text.replace(":",".");
}
Log.i("Tag",text);
Read up on Strings and their immutable property.
Related Topics
Create File with Given Size in Java
What Is the Purpose of Accesstype.Field, Accesstype.Property and @Access
What Do Curly Braces in Java Mean by Themselves
How to Use Argumentcaptor for Stubbing
Exception Noclassdeffounderror for Cacheprovider
How to Disable Loggers of a Class or of Whole Package
Java Swing Jtextfield Set Placeholder
How Can "This" of the Outer Class Be Accessed from an Inner Class
How to Insert a Row Between Two Rows in an Existing Excel with Hssf (Apache Poi)
Dynamic Generic Typing in Java
How to Convert Int[] to Byte[]
Using Variables Outside of an If-Statement
Printing Message on Console Without Using Main() Method
String S = New String("Xyz"). How Many Objects Has Been Made After This Line of Code Execute
What Is the Time Complexity Performance of Hashset.Contains() in Java