i am tired of writing this:
string_needed="prefix....." + topic + "suffix...." + name + "testing";
i would think someone might have done something about this by now ;)
i am tired of writing this:
string_needed="prefix....." + topic + "suffix...." + name + "testing";
i would think someone might have done something about this by now ;)
ES6 added template strings, which use backticks (`) instead of single or double quotes. In a template string, you can use the ${}
syntax to add expressions. Using your example, it would be:
string_needed = `prefix.....${topic}suffix....${name}testing`
Sorry :(
I like to take advantage of Array.join:
["prefix ....", topic, "suffix....", name, "testing"].join("")
or use String.concat
String.concat("a", 2, "c")
or you could write your own concatenate function:
var concat = function(/* args */) {
/*
* Something involving a loop through arguments
*/
}
or use a 3rd-party sprintf
function, such as http://www.diveintojavascript.com/projects/javascript-sprintf
You could consider using coffeescript to write the code (which has interpolation like Ruby ie #{foo}).
It 'compiles' down to javascript - so you will end up with javascript like what you've written, but without the need to write/maintain the +++ code you're tired of
I realize that asking you to consider another language is on the edge of being a valid answer or not but considering the way coffeescript works, and that one of your tags is Ruby, I'm hoping it'll pass.
As a Javascript curiosity, you can implement something that basically does Ruby-like interpolation:
sub = function(str) {
return str.replace(/#\{(.*?)\}/g,
function(whole, expr) {
return eval(expr)
})
}
js> y = "world!"
world!
js> sub("Hello #{y}")
Hello world!
js> sub("1 + 1 = #{1 + 1}")
1 + 1 = 2
Using it on anything but string literals is asking for trouble, and it's probably quite slow anyways (although I haven't measured). Just thought I'd let you know.
Direct answer: No javascript doesn't support string interpolation.
The only way is to implement it yourself or use a third party lib who does it for you.
EDIT
As Marcos added in comments theres a proposal for ECMAScript 6 (Harmony), so we can have proper string interpolation:
var a = 5;
var b = 10;
console.log(`Fifteen is ${a + b} and\nnot ${2 * a + b}.`);
// "Fifteen is 15 and
// not 20."
Please see more information here.
I just wrote this hacky function to do this. Usage is as follows:
interpolate("#{gimme}, #{shelter}", {gimme:'hello', shelter:'world'})
// returns "hello, world"
And the implementation:
interpolate = function(formatString, data) {
var i, len,
formatChar,
prevFormatChar,
prevPrevFormatChar;
var prop, startIndex = -1, endIndex = -1,
finalString = '';
for (i = 0, len = formatString.length; i<len; ++i) {
formatChar = formatString[i];
prevFormatChar = i===0 ? '\0' : formatString[i-1],
prevPrevFormatChar = i<2 ? '\0' : formatString[i-2];
if (formatChar === '{' && prevFormatChar === '#' && prevPrevFormatChar !== '\\' ) {
startIndex = i;
} else if (formatChar === '}' && prevFormatChar !== '\\' && startIndex !== -1) {
endIndex = i;
finalString += data[formatString.substring(startIndex+1, endIndex)];
startIndex = -1;
endIndex = -1;
} else if (startIndex === -1 && startIndex === -1){
if ( (formatChar !== '\\' && formatChar !== '#') || ( (formatChar === '\\' || formatChar === '#') && prevFormatChar === '\\') ) {
finalString += formatChar;
}
}
}
return finalString;
};