Use of String.Format in JavaScript?

后端 未结 19 2028
逝去的感伤
逝去的感伤 2020-12-04 07:59

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

19条回答
  •  情深已故
    2020-12-04 08:45

    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.

提交回复
热议问题