Writing a generic function to format strings using bob.js

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newtojs
    New Member
    • Apr 2016
    • 1

    Writing a generic function to format strings using bob.js

    Hello,

    Code:
    var string = "My name is {0} and I am {1} years old."; 
    var result = bob.string.formatString(sFormat, "myname", 25);
    I want to write a function such that I can pass the string with required number of arguments and it will return me the formatted string with all {0} , {1}, .. replaced by my passed arguments. Something like:

    Code:
    function test( string , argument1 , argument2 , ....){
      return bob.string.formatString(sFormat, args);
    }
    Last edited by Dormilich; Apr 14 '16, 08:33 PM. Reason: please use code tags
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Template Literals (https://developer.mozilla.org/en-US/...plate_literals) look quite close otherwise you’d have to use String.replace( ).

    Comment

    Working...