I'm using javascript and jquery, I have a string like
var apiUrlAnswer = 'https://api.stackexchange.com/2.0/answers/{ids}?order=desc&sort=activity&site=stackoverflow';
I need replace the {ids} with a variable.
In C# is possible to use a method called
String.Format(https://api.stackexchange.com/2.0/answers/{0}?order=desc&sort=activity&site=stackoverflow, myVariable);
I would like to know if exist something similar in Javascript so I can avoid simple string concatenation.
Thanks for your help!
Use the String .replace() method:
var apiUrlAnswer = 'https://api.stackexchange.com/2.0/answers/{ids}?order=desc&sort=activity&site=stackoverflow';
apiUrlAnswer = apiUrlAnswer.replace("{ids}", yourVariable);
// OR, if there might be more than one instance of '{ids}' use a regex:
apiUrlAnswer = apiUrlAnswer.replace(/\{ids\}/g, yourVariable);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With