Kotlin String Interpolation

Strings in Kotlin are essentially identical to Java strings. However, they include some additional features. The string interpolation feature in the Kotlin language allows us to implement expressions in a string, avoiding the need to concatenate expressions using the “+” symbol. In the article, we’ll learn about string templates known as String Interpolation. We have a special type of concept called String Interpolation.

What Is String Interpolation in Kotlin?

A string interpolation or interpolation expression is a piece of code that is evaluated and returns a string as a result. In Kotlin, string interpolation allows us to concatenate constant strings efficiently and variables to create another string. String interpolation is the substitution of a variable’s value within a string. The “$” character is used to interpolate a variable in Kotlin, and the “$” character is used for an expression interpolation.

To understand the Kotlin string interpolation, we have the following different examples:

Example # 1: Program of Using String Interpolation in Kotlin:

The variables can be interpolated within the string, which means their values are substituted. We can obtain the string’s length by including an expression in brackets.

We have the main function defined in which code implementation is done. The main function has a variable as the “val” keyword, which is assigned a name as “myName”. The variable “myName” has been initialized with a string value. The other variable is assigned with the name “myAge”, which contains an integer value. The println function uses the string interpolation feature with the sign “$”. The sign “$” is used just before the variables “myName” and “myAge” in the string of println function. Here, we have the length of the variable “myName” by surrounding it in the curly brackets. There is another variable defined as “myMsg”, which also contains the string value, and in the println function, we are fetching the length of the string with the string interpolation feature.

We have printed the value of the variable and length of the strings by using string interpolation in the string. The output is shown in the following image:

Example # 2: Program of Using String Interpolation for Arithmetic Operation Evaluation in Kotlin:

We can also apply string interpolation for the evaluations of the arithmetic operations. Let’s see how the string interpolation works with the arithmetic operations.

We have first defined the main function. The main function has variables represented with the keyword “var”. We have created a variable with the name “a”, which has an “Int” type property and is initialized with the number “10”. We have another variable created as “b” with the “Int” property and contains a number value “5”. There are other variables defined as “sum”, “sub”, “multiply”, and “divide” in which we have assigned basic arithmetic operations. Then, we have the Kotlin println function, which contains the string.

In the following string, we have used string interpolation on specified variables such that they are substituted with their values:

Example # 3: Program of Reassigning a String Variable With String Interpolation in Kotlin:

You can’t modify a string’s characters. However, if we declare a string variable using the keyword “var,” we can reassign it again.

We have the main function declaration. Here, we have created a variable with the keyword “var”. The variable is defined as “stringIs” and initialized with the string value. The variable is interpolated within the string as “$stringIs”. Then, we have reassigned a string to a variable “stringIs”. We used a “var” reference that is mutable and allows us to modify an existing string. The println has variable string interpolation, which will be used against the variable “stringIs” value.

We have an output of the previous string and the updated string on the following terminal screen:

Example # 4: Program of Using String Interpolation With Raw String in Kotlin:

The raw string is delimited with the triple quotes (“““……”””) and it does not contain escape characters. It provides the facility of writing the string into multiple lines, which is also called a multi-line string.

We have defined our main function. Then, we have defined a variable with the “val” keyword. The variable is assigned as “x”, containing a numeric value “9”. We have also created a variable as “y”, which also includes a numeric value “3”. We are using a delimiter with triple quotes, which are stored in the variable “myStr” and use a string interpolated expression. Note that we have a conditional statement of getting the max value from the specified variables. The “$expression”, which encloses the expression in curly brackets, can be used to resolve any ambiguities or to employ more complex expressions. Then, the println function has string interpolation expression, which has trimMargin() function. The trimMargin uses “|” and removes the leading white spaces from the raw text.

We have the output of the raw string in the following image:

Example # 5: Program of Using String Interpolation With String Properties in Kotlin:

We have used properties and several string class methods as the string literals in Kotlin are members of this class. The following method is used in the example as shown below:

1. compareTo(): It compares the given string and returns “0” if the strings are the same.

2. get(): The specific index value of the character is returned in this method.

3. plus(): This function returns a new string created by concatenating this string with the string supplied to it.

4. subSequence(start, end): Returns a substring that starts at the start and ends at the end but does not include the end.

We have a main function defined in which we have used a few string methods. First, we have created a variable as “str1” and a variable as “str2”, which are initialized with string values. Then, we have another variable as “Result”, which has string type property. The “Result” variable contains the compareTo() method, which compares whether str1 and str2 are equal to or not. Then, we have a println function with string interpolation and string interpolation expression in the string. We have used the get() method and subsequence method, which will be printed as an output.

The output of using a different string class method in string literals is displayed below:


The article’s goal was to show readers how to use string interpolation and expression in the string. The string formatting in Kotlin is more efficient than simple interpolation. We have different examples of using string Interpolation in Kotlin. These examples will help you in the string interpolation of the Kotlin programming language. We hope you found this article helpful. Check the other Linux Hint articles for more tips and information.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.