Questions tagged [string-interpolation]

String interpolation is the replacement of defined character sequences in a string by given values. This representation may be considered more intuitive for formatting and defining content than the composition of multiple strings and values using concatenation operators. String interpolation is typically implemented as a language feature in many programming languages including PHP, Haxe, Perl, Ruby, Python, C# (as of 6.0) and others.

https://en.wikipedia.org/wiki/String_interpolation

881 questions
633
votes
20 answers

How can I do string interpolation in JavaScript?

Consider this code: var age = 3; console.log("I'm " + age + " years old!"); Are there any other ways to insert the value of a variable in to a string, apart from string concatenation?
Tom Lehman
  • 75,197
  • 69
  • 188
  • 262
513
votes
16 answers

How to interpolate variables in strings in JavaScript, without concatenation?

I know in PHP we can do something like this: $hello = "foo"; $my_string = "I pity the $hello"; Output: "I pity the foo" I was wondering if this same thing is possible in JavaScript as well. Using variables inside strings without using…
DMin
  • 8,279
  • 9
  • 41
  • 61
458
votes
1 answer

How to use the ternary operator inside an interpolated string?

I'm confused as to why this code won't compile: var result = $"{fieldName}{isDescending ? " desc" : string.Empty}"; If I split it up, it works fine: var desc = isDescending ? " desc" : string.Empty; var result = $"{fieldName}{desc}";
Nate Barbettini
  • 43,095
  • 21
  • 121
  • 135
433
votes
15 answers

PHP - how to create a newline character?

In PHP I am trying to create a newline character: echo $clientid; echo ' '; echo $lastname; echo ' '; echo '\r\n'; Afterwards I open the created file in Notepad and it writes the newline literally: 1 John Doe\r\n 1 John Doe\r\n 1 John Doe\r\n I…
davidjhp
  • 7,086
  • 8
  • 29
  • 52
354
votes
9 answers

Is there a Python equivalent to Ruby's string interpolation?

Ruby example: name = "Spongebob Squarepants" puts "Who lives in a Pineapple under the sea? \n#{name}." The successful Python string concatenation is seemingly verbose to me.
Caste
  • 3,543
  • 2
  • 13
  • 5
219
votes
5 answers

String variable interpolation Java

String building in Java confounds me. I abhore doing things like: url += "u1=" + u1 + ";u2=" + u2 + ";u3=" + u3 + ";u4=" + u4 + ";"; url += "x=" + u1 + ";y=" + u2 + ";z=" + u3 + ";da1=" + u4 + ";"; url += "qty=1;cost=" + orderTotal + ";ord=" +…
cmcculloh
  • 43,791
  • 36
  • 94
  • 126
176
votes
2 answers

How to perform string interpolation in TypeScript?

C# uses string interpolation int value = 100; Console.WriteLine($"The size is {value}."); Output: The size is 100. How to do the same thing in TypeScript?
zshanabek
  • 2,625
  • 3
  • 14
  • 22
148
votes
1 answer

How to use verbatim strings with interpolation?

In C# 6 there is a new feature: interpolated strings. These let you put expressions directly into code. Rather than relying on indexes: string s = string.Format("Adding \"{0}\" and {1} to foobar.", x, this.Y()); the above becomes: string s =…
Keith
  • 133,927
  • 68
  • 273
  • 391
134
votes
1 answer

Escape a dollar sign in string interpolation

How do I escape a dollar sign in string interpolation? def getCompanion(name: String) = Class.forName(s"my.package.$name\$") // --> "error: unclosed string literal"
0__
  • 64,257
  • 16
  • 158
  • 253
125
votes
4 answers

String Interpolation vs String.Format

Is there a noticable performance difference between using string interpolation: myString += $"{x:x2}"; vs String.Format()? myString += String.Format("{0:x2}", x); I am only asking because Resharper is prompting the fix, and I have been fooled…
Krythic
  • 3,334
  • 5
  • 20
  • 59
117
votes
10 answers

How to postpone/defer the evaluation of f-strings?

I am using template strings to generate some files and I love the conciseness of the new f-strings for this purpose, for reducing my previous template code from something like this: template_a = "The current name is {name}" names = ["foo",…
JDAnders
  • 2,311
  • 4
  • 8
  • 8
114
votes
4 answers

How can you use an object's property in a double-quoted string?

I have the following code: $DatabaseSettings = @(); $NewDatabaseSetting = "" | select DatabaseName, DataFile, LogFile, LiveBackupPath; $NewDatabaseSetting.DatabaseName = "LiveEmployees_PD"; $NewDatabaseSetting.DataFile =…
caveman_dick
  • 5,322
  • 3
  • 27
  • 46
110
votes
15 answers

How do I interpolate strings?

I want to do the following in C# (coming from a Python background): strVar = "stack" mystr = "This is %soverflow" % (strVar) How do I replace the token inside the string with the value outside of it?
sazr
  • 21,294
  • 58
  • 170
  • 304
98
votes
11 answers

How to substitute shell variables in complex text files

I have several text files in which I have introduced shell variables ($VAR1 or $VAR2 for instance). I would like to take those files (one by one) and save them in new files where all variables would have been replaced. To do this, I used the…
Ben
  • 4,821
  • 5
  • 33
  • 60
95
votes
8 answers

How to solve "String interpolation produces a debug description for an optional value; did you mean to make this explicit?" in Xcode 8.3 beta?

Since beta 8.3, zillions warnings "String interpolation produces a debug description for an optional value; did you mean to make this explicit?" appeared in my code. For example, the warning popped in the following situation up, where options could…
Stéphane de Luca
  • 10,239
  • 7
  • 45
  • 74
1
2 3
58 59