This is driving me nuts. I believe I asked this exact same question, but I can\'t find it any more (I used Stack Overflow search, Google Search, manually searched my po
Just make and use this function:
function format(str, args) {
for (i = 0; i < args.length; i++)
str = str.replace("{" + i + "}", args[i]);
return str;
}
If you don't want to change the str parameter, then before the for
loop, clone (duplicate) it to a new string (make a new copy of str), and set the copy in the for
loop and at last return it instead of the parameter itself.
In C# (Sharp) it is simple create to a copy by just calling String.Clone()
, but I don't know how in JavaScript, but you can search on Google or surf on the Internet and learn ways to do it.
I just gave you my idea about string format in JavaScript.