In Javascript, the method to print out using
console.log("this is %s and %s", foo, bar);
works, so it follows some C style, but it doesn’t follow
console.log("%*s this is %s and %s", 12, foo, bar);
where the %*s and 12 is to make it print out 12 spaces, as in this question: In Objective-C, how to print out N spaces? (using stringWithCharacters)
Is there a short, quick way to make it work simply in Javascript? (say, without using the sprintf open source library or writing a function to do it?)
Update:, in my case, 12 is actually a variable, such as (i * 4), so that’s why it can’t be hardcoded spaces inside the string.
The easiest way would be to use Array.join:
Note that you want N + 1 for the array size.
I know you said you dont want functions, but if you do this a lot, an extention method can be cleaner:
This allows you to do: