-1

What is the easiest / most concise way to inject values into a string without using a RegEx expression or complex function?

For example, this:

var a = 'cats';
var b = 'dogs';

var result = String.format('%a and %b living together', a, b);
console.log(result);

... should yield ...

cats and dogs living together

I'm coming from the C# world where this is ridiculously easy. Every search on SO turns up some type of overcomplicated RegEx expression or replace function.

G. Deward
  • 1,422
  • 2
  • 17
  • 27

1 Answers1

3

Template literals is probably the most concise (require ES6).

var result = `${a} and ${b} living together`;

On the other hand, regular string concatenation isn't that bad either.

var result = '' + a + ' and ' + b + ' living together';
Gilles Quenot
  • 154,891
  • 35
  • 213
  • 206
Joseph
  • 113,089
  • 28
  • 177
  • 225