javascript - ToString() and .replace in one line? - Stack Overflow

Is it possible to bine these two lines of code into one?Meaning, can we do a ToString() and .replace()

Is it possible to bine 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");

Is it possible to bine 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");
Share Improve this question asked Jan 25, 2016 at 3:17 ConfusedDeerConfusedDeer 3,4158 gold badges46 silver badges77 bronze badges 2
  • 1 Have you actually tried? – PM 77-1 Commented Jan 25, 2016 at 3:21
  • What is myFirstVar value? – Tushar Commented Jan 25, 2016 at 3:25
Add a ment  | 

2 Answers 2

Reset to default 5

Yes, you can chain the methods.

myFirstVar.toString().replace('a', 'u')

Note:

  1. ToString should be toString
  2. String#replace will 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");

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744272374a4566163.html

相关推荐

  • javascript - ToString() and .replace in one line? - Stack Overflow

    Is it possible to bine these two lines of code into one?Meaning, can we do a ToString() and .replace()

    7天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信