Tostring() And. Replace in One Line?
Is It Possible to Combine These Two Lines of Code into One? Meaning, Can We Do a Tostring() And. Replace() in One Line? Var mySecondVar = myFirstVar...
Is it possible to combine these two lines of code into one?
Meaning, can we do a ToString() and .replace() in one line?
var mySecondVar = myFirstVar.ToString()
mySecondVar = mySecondVar.replace("a","u");
2 Answers
Yes, you can chain the methods.
myFirstVar.toString().replace('a', 'u')
Note:
ToStringshould betoStringString#replacewill only replace the first occurrence of the string. To replace all occurrences use replace with RegEx
You can use:
var mySecondVar = myFirstVar.toString().replace("a","u");