String Interpolation: Difficult to say, easy to implement.

In this ultimate guide, I seek to help you implement string interpolation in any language... well almost any language, the list of language specific implementations will continue to grow!

Want me to add a language that is missing from the list? Tweet @ Me

What is String Interpolation in Software Development?

String Interpolation is the act of inserting variables into a string value in one line of code without adding concatenation indicators in-between your variables throughout a string variable.

String Interpolation is generally preferred over string concatenation.

Clear as mud? Here is a more official definition of String interpolation from Wikipedia:

In computer programming, string interpolation (or variable interpolation, variable substitution, or variable expansion) is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.

It is a form of simple template processing or, in formal terms, a form of quasi-quotation (or logic substitution interpretation).

String interpolation allows easier and more intuitive string formatting and content-specification compared with string concatenation.

When I am confused by a phrase or term that is used within the Software Development community, I tend to 'anchor' to the origin of a word to better understand the concept when it is used in the context of a programming pattern or paradigm.

Here is the definition of Interpolation from Google:

  1. the insertion of something of a different nature into something else
  2. a remark interjected in a conversation

How Do You Pronounce 'String Interpolation'?

Here is a simple demonstration to show the pronunciation of 'String Interpolation': string uhn·tur·puh·lay·shn

Here is the official phonetic transcription of the word's pronunciation via Google: /inˌtərpəˈlāSH(ə)n/

The phrase 'String Interpolation' will get mentioned many times at your job, it's a phrase that many people struggle to say correctly (it's a mouthful) so don't feel bad if you pronounce it incorrectly.

List of Languages that Support String Interpolation

Well that's enough talk about the definition and pronunciation, let's get into the actual code examples.

Here is a table of languages that support String Interpolation, feel free to click on the links in the 'String Interpolation Examples' column to jump down to the specific examples for a given language.

Note: I am probably missing some languages but I will add them as a I come across more languages!

Want me to add a language that is missing from this list? Tweet @ Me

Language String Interpolation Examples
C# C# - String Interpolation Examples

Want me to add a language that is missing from this list? Tweet @ Me

C# - How to Use String Interpolation in C#

In C# there are two types of string interpolation syntaxes:

  1. Composite Formatting
  2. Interpolated Strings

Interpolated strings are easier to read versus composite formatting, you may disagree, but I prefer using interpolated strings over composite formatting.

Composite Formatting Example

var firstName = "Brendan";
var lastName = "Sluke";

// Here is the composite formatting syntax in action
Console.WriteLine("Hello, my name is {0} {1}.", firstName, lastName);

// Expected output
// Hello, my name is Brendan Sluke.

Interpolated String Example

$ is the special character that indicates an interpolated string in C#. String interpolation was first introduced in C# 6.

var firstName = "Brendan";
var lastName = "Sluke";

// Here is the interpolating string syntax in action
Console.WriteLine($"Hello, my name is {firstName} {lastName}.");

// Expected output
// Hello, my name is Brendan Sluke.

You can see from this example that Interpolated Strings are generally easier to read versus composite formatting.

Special Characters in C# Interpolated Strings

Let's say that you want to use a double quote " or a brace { in your interpolated string. In this case you need to escape any special characters.

Double quotes are escaped with a leading backslash \", braces should be doubled up {{ and }} in order to escape the brace character.

var firstName = "Brendan";
var lastName = "Sluke";

// Here is an example of escaping special characters inside of interpolated strings
Console.WriteLine($"Hello, are you: \"{firstName} {lastName}\"? {{Type Y/N}}");

// Expected Output
// Hello, are you "Brendan Sluke"? {Type Y/N}

C# Multi Line Interpolated Strings

There are cases where your string is very long, and you want to start it on a new line to make your code more readable for other developers.

In C#, for multi-line strings you preface your string with an @ symbol.

Example of a multi- line string in C#:

var firstName = "Brendan";
var lastName = "Sluke";

// Here is a basic example of a multi line string
var multiLineString = @"Hello
                        World";
Console.WriteLine(multiLineString);

// Expected Output
// Hello World

For multi-line interpolated strings you will use the [email protected]"..." syntax.

Note: Starting in C# 8 you can use either [email protected]"..." or @$"..." syntax for interpolated strings. But in prior versions $ must proceed @.

// Here is an example of a multi line string in versions prior to C# 8.0
Console.WriteLine([email protected]"Hello,
                  {firstName} {lastName}.");

// Expected Output
// Hello Brendan Sluke

// Here is an example syntax that can be used in C# 8.0
// Notice the @ is before the $ in this example!
Console.WriteLine(@$"Hello,
                  {firstName} {lastName}.");

// Expected Output
// Hello Brendan Sluke

For further reading, please reference: Microsoft's Document on Interpolated Strings

FeedbackI'd love to hear your feedback!
Did this blog post help fix your issue? Still having trouble?
Tweet At Me
Brendan SlukeSoftware Engineer

Hi, I'm Brendan Sluke and I love writing code and blogging about software engineering.

Follow Me On Twitter Read My Blog
Recommended Blog PostsBlog Posts I Think You Will Find Interesting
Go Back To Top of Page