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
.
Why isn't the replace() function working?
strings are immutable. so header_raw_text.replace()
does not change the string itself.you have to do reassign the result after replacing.
header_raw_text = header_raw_text.replace("arrow_upward ", "")
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
replace() method not working on Pandas DataFrame
You need to assign back
df = df.replace('white', np.nan)
or pass param inplace=True
:
In [50]:
d = {'color' : pd.Series(['white', 'blue', 'orange']),
'second_color': pd.Series(['white', 'black', 'blue']),
'value' : pd.Series([1., 2., 3.])}
df = pd.DataFrame(d)
df.replace('white', np.nan, inplace=True)
df
Out[50]:
color second_color value
0 NaN NaN 1.0
1 blue black 2.0
2 orange blue 3.0
Most pandas ops return a copy and most have param inplace
which is usually defaulted to False
replace() function not working as intended
String.prototype.replace()
The
replace()
method returns a new string with some or all matches of a pattern replaced by a replacement.
You have to reassign the new value to the variable (word).
const getLetterCount = (stringToTest) => {
const wordArray = stringToTest.split('');
let totalLetters = 0;
for (let word of wordArray) {
word = word.replace(/[.,\/#!$%\^&\*;:{}=\-_` ~()]/g, "");
totalLetters += word.length;
}
console.log(totalLetters);
}
getLetterCount('boy/girl?') // returns 9 ( counting punctuation as well)
Why my contains and replace method doesn't work right?
Change the replace method call to:
str1 = str1.replace("replace", "Hi");
Since String
are immutable, you need to reassign the result back to str1
. It doesn't perform in-place replacement, rather it constructs and returns a new String object. the original String is unmodified.
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")
JavaScript replace doesn't work
barea = barea.replace(area, "cu")
You need to assign it since String.prototype.replace
isn't a mutator method.
Related Topics
How to Group an Array of Objects by Key
How to Get the Value of Text Input Field Using JavaScript
Short Circuit Array.Foreach Like Calling Break
How to Split a String, Breaking At a Particular Character
Find the Min/Max Element of an Array in JavaScript
Executing ≪Script≫ Elements Inserted With .Innerhtml
Choosing and Activating the Right Controls on an Ajax-Driven Site
How to Access Post Form Fields in Express
How to Stop the Browser Back Button Using JavaScript
How to Add 1 Day to Current Date
How to Attach Events to Dynamic HTML Elements With Jquery
Why Does Calling React Setstate Method Not Mutate the State Immediately
JavaScript Object: Access Variable Property by Name as String
Why Is the Method Executed Immediately When I Use Settimeout
Window.Close and Self.Close Do Not Close the Window in Chrome
JavaScript Raises Syntaxerror With Data Rendered in Jinja Template