4

Is there a another way to do this? I'm using the underscore js lib here, which is where the _. comes from. I'm used to this sort of procedure to format strings in python and I'd like something simple for JavaScript without doing + + + + all the time. This works, but it feels like I reinvented the wheel.

function foo (iterable, string) { var s = iterable.shift(); string = string.replace("%s",s); return _.isEmpty(iterable) ? string : foo(iterable,string); }; foo(['sam','green','ham'],"%s likes %s eggs and %s."); "sam likes green eggs and ham." 
0

1 Answer 1

0

try out the sprintf lib, specifically vsprintf.

vsprintf('The first 4 letters of the english alphabet are: %s, %s, %s and %s', ['a', 'b', 'c', 'd']); 
Sign up to request clarification or add additional context in comments.

1 Comment

Since the link has rotted (yet another warning against linking to libraries rather than including their source where possible), there's this question's answers..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.