C# interpolated string, JavaScript template literal
-
You probably know about C# String interpolation and the new(er) (C# 6, via .NET 4.6) string operator $. Here's the explanation and sample from ($ - string interpolation (C# Reference) | Microsoft Docs[^])
string name = "Mark";
var date = DateTime.Now;// Composite formatting:
Console.WriteLine("Hello, {0}! Today is {1}, it's {2:HH:mm} now.", name, date.DayOfWeek, date);
// String interpolation:
Console.WriteLine($"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now.");
// Both calls produce the same output that is similar to:
// Hello, Mark! Today is Wednesday, it's 19:40 now.The new formatting is very nice, much more straight forward and obvious about which values are included in the string. JavaScript Analog But, did you know about the JavaScript equivalent that entered in JavaScript 2015? I just saw this for the first time -- only three years late. :laugh: JavaScript calls them template literals. The very odd thing is that you have to use back-ticks to get them to work.
var who = "world";
console.log(`hello, ${who}`);That produces an output of :
hello, world
If you use double or single quotes instead of back-ticks it doesn't work. The back-ticks kind of make me shudder. :| It is a bit nicer than concatenating strings and vars with all those + signs though. Previously you had to do something like:
var who = "world";
console.log("hello, " + who);You can read more about template literals here[^]. It's interesting to see a concept emerge and then how different language developers implement it. C# Bonus Weirdness You can use the verbatim string operator with the interpolated string operator like the following:
string finalAnswer = $@"this is the answer to the question: {40+2}";
Console.WriteLine(finalAnswer);But if you place the verbatim string operator before the interpolated string operator you will get a syntax error.
string finalAnswer = @$"thi
-
You probably know about C# String interpolation and the new(er) (C# 6, via .NET 4.6) string operator $. Here's the explanation and sample from ($ - string interpolation (C# Reference) | Microsoft Docs[^])
string name = "Mark";
var date = DateTime.Now;// Composite formatting:
Console.WriteLine("Hello, {0}! Today is {1}, it's {2:HH:mm} now.", name, date.DayOfWeek, date);
// String interpolation:
Console.WriteLine($"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now.");
// Both calls produce the same output that is similar to:
// Hello, Mark! Today is Wednesday, it's 19:40 now.The new formatting is very nice, much more straight forward and obvious about which values are included in the string. JavaScript Analog But, did you know about the JavaScript equivalent that entered in JavaScript 2015? I just saw this for the first time -- only three years late. :laugh: JavaScript calls them template literals. The very odd thing is that you have to use back-ticks to get them to work.
var who = "world";
console.log(`hello, ${who}`);That produces an output of :
hello, world
If you use double or single quotes instead of back-ticks it doesn't work. The back-ticks kind of make me shudder. :| It is a bit nicer than concatenating strings and vars with all those + signs though. Previously you had to do something like:
var who = "world";
console.log("hello, " + who);You can read more about template literals here[^]. It's interesting to see a concept emerge and then how different language developers implement it. C# Bonus Weirdness You can use the verbatim string operator with the interpolated string operator like the following:
string finalAnswer = $@"this is the answer to the question: {40+2}";
Console.WriteLine(finalAnswer);But if you place the verbatim string operator before the interpolated string operator you will get a syntax error.
string finalAnswer = @$"thi
Quote:
if you place the verbatim string operator before the interpolated string operator you will get a syntax error.
That's because the verbatim operator is tightly couple to the string and suppresses interpolation of the string - therefore the interpolation operator needs to come after the resulting string from the "non-interpolation" operator. ...erm, or maybe the other way around? Whatevs!
- I would love to change the world, but they won’t give me the source code.
-
Quote:
if you place the verbatim string operator before the interpolated string operator you will get a syntax error.
That's because the verbatim operator is tightly couple to the string and suppresses interpolation of the string - therefore the interpolation operator needs to come after the resulting string from the "non-interpolation" operator. ...erm, or maybe the other way around? Whatevs!
- I would love to change the world, but they won’t give me the source code.
-
Yeah, the way I saw it explained was : $ then @ because you are saying, "interpolate this verbatim string..." Instead of @ then $ "Give me verbatim of this interpolated string..." Kind of makes sense, I guess.
raddevus wrote:
Kind of makes sense, I guess.
I promise to quote you verbatim without interpolation. ;)
Latest Article - A Concise Overview of Threads Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802
-
raddevus wrote:
Kind of makes sense, I guess.
I promise to quote you verbatim without interpolation. ;)
Latest Article - A Concise Overview of Threads Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802
-
You probably know about C# String interpolation and the new(er) (C# 6, via .NET 4.6) string operator $. Here's the explanation and sample from ($ - string interpolation (C# Reference) | Microsoft Docs[^])
string name = "Mark";
var date = DateTime.Now;// Composite formatting:
Console.WriteLine("Hello, {0}! Today is {1}, it's {2:HH:mm} now.", name, date.DayOfWeek, date);
// String interpolation:
Console.WriteLine($"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now.");
// Both calls produce the same output that is similar to:
// Hello, Mark! Today is Wednesday, it's 19:40 now.The new formatting is very nice, much more straight forward and obvious about which values are included in the string. JavaScript Analog But, did you know about the JavaScript equivalent that entered in JavaScript 2015? I just saw this for the first time -- only three years late. :laugh: JavaScript calls them template literals. The very odd thing is that you have to use back-ticks to get them to work.
var who = "world";
console.log(`hello, ${who}`);That produces an output of :
hello, world
If you use double or single quotes instead of back-ticks it doesn't work. The back-ticks kind of make me shudder. :| It is a bit nicer than concatenating strings and vars with all those + signs though. Previously you had to do something like:
var who = "world";
console.log("hello, " + who);You can read more about template literals here[^]. It's interesting to see a concept emerge and then how different language developers implement it. C# Bonus Weirdness You can use the verbatim string operator with the interpolated string operator like the following:
string finalAnswer = $@"this is the answer to the question: {40+2}";
Console.WriteLine(finalAnswer);But if you place the verbatim string operator before the interpolated string operator you will get a syntax error.
string finalAnswer = @$"thi
raddevus wrote:
But if you place the verbatim string operator before the interpolated string operator you will get a syntax error.
Looks like that should be "fixed" in C# 8: Implement verbatim interpolated strings by jcouv · Pull Request #28355 · dotnet/roslyn · GitHub[^] Champion: verbatim interpolated string (order of $@ vs. @$) · Issue #1630 · dotnet/csharplang · GitHub[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
raddevus wrote:
But if you place the verbatim string operator before the interpolated string operator you will get a syntax error.
Looks like that should be "fixed" in C# 8: Implement verbatim interpolated strings by jcouv · Pull Request #28355 · dotnet/roslyn · GitHub[^] Champion: verbatim interpolated string (order of $@ vs. @$) · Issue #1630 · dotnet/csharplang · GitHub[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer