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.
Questions tagged [string-interpolation]
1093 questions
774
votes
22 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
- 85,973
- 71
- 200
- 272
765
votes
9 answers
Remove a fixed prefix/suffix from a string in Bash
I want to remove the prefix/suffix from a string. For example, given:
string="hello-world"
prefix="hell"
suffix="ld"
How do I get the following result?
"o-wor"

Dušan Rychnovský
- 11,699
- 8
- 41
- 65
672
votes
17 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
- 10,049
- 10
- 45
- 65
592
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
- 51,256
- 26
- 134
- 147
488
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,816
- 9
- 36
- 56
363
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,633
- 2
- 15
- 5
321
votes
3 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
- 4,270
- 3
- 19
- 27
279
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
- 47,596
- 40
- 105
- 130
183
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
- 150,284
- 78
- 298
- 434
179
votes
15 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
- 4,831
- 4
- 10
- 8
176
votes
7 answers
String Interpolation vs String.Format
Is there a noticeable 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
- 4,184
- 5
- 26
- 67
151
votes
12 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
- 6,321
- 9
- 40
- 76
147
votes
5 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
- 6,302
- 3
- 34
- 49
142
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__
- 66,707
- 21
- 171
- 266
141
votes
12 answers
String replacement in java, similar to a velocity template
Is there any String replacement mechanism in Java, where I can pass objects with a text, and it replaces the string as it occurs?
For example, the text is:
Hello ${user.name},
Welcome to ${site.name}.
The objects I have are user and site. I want…

Sastrija
- 3,284
- 6
- 47
- 64