The .replace() method does change the string in place
replace()
(a JavaScript function, not jQuery) returns a string, try this :
var valr='r';
valr = valr.replace('r', 't');
$('.try').prepend('<div> '+valr+'</div>');
Docs for .replace()
are here
Why isn't the String value changed after String.Replace in Java?
String are immutable in Java, which means you cannot change them. When you invoke the replace method you are actually creating a new String.
You can do the following:
String str = "Test";
str = str.replace('T', 'B');
Which is a reassignment.
Does javascript have a method to replace part of a string without creating a new string?
No, strings in JavaScript are immutable.
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", " ");
Python String replace doesn't work
Strings are immutable. That means that they cannot be changed. stringT.replace(...)
does not change stringT
itself; it returns a new string. Change that line to:
stringT = stringT.replace("world", "all")
Why replace() doesn't work in my Python function?
replace
is not a in-place method, but instead it returns a new string, so you need to assign the result to a new string.
From the docs: https://docs.python.org/3/library/stdtypes.html#str.replace
str.replace(old, new[, count])
Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.
Also your logic can be simplified a lot like below, if you iterate on key and value together
def replace_exception_chars(string):
exception_chars_dict = {'Old': 'New', 'old': 'new'}
#Iterate over key and value together
for key, value in exception_chars_dict.items():
#If key is found, replace key with value and assign to new string
if key in string:
string = string.replace(key, value)
return string
print(replace_exception_chars('Old, not old'))
The output will be
New, not new
`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");
Difference between String replace() and replaceAll()
In java.lang.String
, the replace
method either takes a pair of char's or a pair of CharSequence
's (of which String is a subclass, so it'll happily take a pair of String's). The replace
method will replace all occurrences of a char or CharSequence
. On the other hand, the first String
arguments of replaceFirst
and replaceAll
are regular expressions (regex). Using the wrong function can lead to subtle bugs.
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.
Related Topics
Javascript:Send JSON Object with Ajax
What's the Difference Between Returning Value or Promise.Resolve from Then()
How to Read a Httponly Cookie Using JavaScript
Protractor E2E Test Case for Downloading PDF File
Why Doesn't Decodeuri("A+B") == "A B"
Jquery Scrolltop() Doesn't Seem to Work in Safari or Chrome (Windows)
Adding/Removing Items from a JavaScript Object with Jquery
Get Word Under Tap from Uiwebview Using JavaScript
Set a Request Header in JavaScript
Why Does JavaScript's Eval Need Parentheses to Eval JSON Data
What Is the $$Hashkey Added to My JSON.Stringify Result
How to Use JavaScript Variables in Ruby
The Invocation Context (This) of the Foreach Function Call