String.Replace (Or Other String Modification) Not Working

string.Replace (or other string modification) not working

Strings are immutable. The result of string.Replace is a new string with the replaced value.

You can either store result in new variable:

var newString = someTestString.Replace(someID.ToString(), sessionID);

or just reassign to original variable if you just want observe "string updated" behavior:

someTestString = someTestString.Replace(someID.ToString(), sessionID);

Note that this applies to all other string functions like Remove, Insert, trim and substring variants - all of them return new string as original string can't be modified.

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

Replace method is not replacing characters in a string

Just replace this line:

keyEdit.Replace('j', 'i');

with this:

keyEdit=keyEdit.Replace('j', 'i');

Returns a new string in which all occurrences of a specified string in
the current instance are replaced with another specified string. MSDN

String.replace() method not working

It seems strDetails does'nt have strDataKey2 when you are doing 2nd replace.
Try resetting like below and see if it works

String strDetails = PropertyLoader.get("details");
String strDataKey1= PropertyLoader.get("datakey1");
String strDataKey2 = PropertyLoader.get("datakey2");
String strDetailsOrg = strDetails;

strDetails = strDetails.replace(strDataKey1, "Programmatically Generated Value 1");

strDetailsOrg = strDetailsOrg.replace(strDataKey2, "Programmatically Generated Value 2");

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.

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` doesn’t change the variable

According to the Javascript standard, String.replace isn't supposed to modify the string itself. It just returns the modified string. You can refer to the Mozilla Developer Network documentation for more info.

You can always just set the string to the modified value:

variableABC = variableABC.replace('B', 'D')

Edit: The code given above is to only replace the first occurrence.

To replace all occurrences, you could do:

 variableABC = variableABC.replace(/B/g, "D");  

To replace all occurrences and ignore casing

 variableABC = variableABC.replace(/B/gi, "D");  

Method string.Replace( ) doesn't work

The replace method returns a new string object, it does not modify the existing string. The following statement should work:

a = a.Replace("=", "r");

String replace method is not replacing characters

And when I debug this the logic does fall into the sentence.replace.

Yes, and then you discard the return value.

Strings in Java are immutable - when you call replace, it doesn't change the contents of the existing string - it returns a new string with the modifications. So you want:

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

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

Note that you don't really need to do this in a condition - after all, if the sentence doesn't contain "and", it does no harm to perform the replacement:

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

String Replacement Error: id.replace(ch, ) not working

Why don't you put all the characters that you want to replace in a separate string and use regex to replace them all from your string?

String charsToBeReplaced = "[!\"#$%&'()*+,-.\\/0123456789:;<=>?@]"; //your ASCII 33 to 64 characters... notice that forward slash and double quotes is escaped here

for (int i=0; i<stringArray.length; i++){
stringArray[i] = stringArray[i].replaceAll(charsToBeReplaced, "");
}

DEMO



Related Topics



Leave a reply



Submit