ToString() and .replace in one line?
Matthew Martinez
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 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");