FREELessons: 20Length: 1.7 hours

• Overview
• Transcript

# 3.2 String Templates and Interpolation

In this lesson, I describe how to use string interpolation. I'll show you some examples to explain the concept.

## 5.Conclusion1 lesson, 01:17

### 3.2 String Templates and Interpolation

Hello, all. In this lesson, we will be learning about string templates and interpolation. This is one of the very beautiful features that Kotlin has provided us with. Here, I have already written a few codes. Let me explain, what is it? In the main function I have defined a variable element and assigned a value of square to it and it is of string type. Because we have initialized element here itself it gives us a warning. It says explicitly given type is redundant here, so we can remove this data type and assign the value directly to element variable. We have the class Geometry here, for which I have instantiated an object objGeometry. In the last lesson, we had studied about constructor. So here we have the constructor of the Geometry class, which accepts a parameter figure, which is again of the string type. So here, while instantiating the object of Geometry class, I have passed a variable of string type, I have passed element. And then with the help of the object of Geometry class, I have called the display function of Geometry class. Now let us run our code so as to ensure that everything is fine as of now. So here we have successfully run our code, it says element is square. Now let us jump into the main topic. How do we use the string template in Kotlin? So we have displayed element as figure. There I have used the traditional way of using the plus operator to contact and aid the strings. But here with Kotlin, we are to use the string interpolation. For string interpolation, what we need to do is prepend the dollar sign with the variable name, and here we have element is dollar figure. This is the variable and here the dollar operator. Let me change it to equal to and now let us run our code and see if all goes well. Shift F10, So here we have element is square. So we have successfully used string interpolation. What if we want to print the length of the element variable which has the string square? We will first see the register method of doing it, and then we will jump to string interpolation. I will comment this out. Now running it, We get length of the string is six. Since it has six characters we get the length of the string is six. Now let us use string interpolation to print this. Let's see what we get. Here we get length of the string is square.length. Why do we get this? We should get six, right? Yes, we need six. But what happens here is the variable element is taken for interpolation, but .length is treated as string. So to impose interpolation in the entire element.length, we need to put it inside the curly braces. And then prepend the $operator with this expression. Now let us run our code and see if we get the correct output. And yes, we have length of the string is six. Samely, if we want to perform some mathematical operations, let's say we need to find out the area of the square whose side is four. And now we need to find out the area of the square. So we write, We will write it this way. Side into side will be in the curly braces, so this entire thing is interpolated. Now let's run our code and see what happens. So here we have area of the square is 16. Now let us try something else. Let us find out the area of the square using the function in the geometry class. Let us change our class name to Square, so that it becomes a little more meaningful, since we need to find out the area of the square. Since the element is square, we will take its side as the parameter, which will be of integer type. We will remove the display function. I remove these two print statements. And here we have the element as squared, the side as four, and we have the object instantiated as the square class and passing the side variable as parameter to it. Let us create a function area which will written as the area of the square. Here in the area function we have a variable a, which is assigned to the area of the square, which is side into side. And then it returns the area of the square, which is of integer type. Therefore, we have the written type for area function as integer here. Now let us call this method from our main method. Here I have called the area function using the object of the class square, and I have assigned this value which is returned from the area function to area square. And then I'll have printed the area of the square. Now let us see what happens. We have successfully printed the area of the square as 16. How can we optimize it a little more? Instead of assigning the value of area to a variable, we can directly call this from our print statement itself. So instead of area square, we write this, and we will remove this line of code. So here we have area of the square is$objSquare.area. Let us see what happens now. This is what happens, objSquare is the object of the square class, hence we have Square here. This is some garbage value which Kotlin returns, and here .area is treated as a string. We have already seen this example with the length function. So what do we do here? We put this inside the curly braces. So let us now run our code and see what happens. Now we get the desired output. The area of the square is 16. This way we have called the area function in the print statement itself and carried out interpolation in it and printed the output. So these are the few examples how we can use the string template. If we have an expression or if we are using some function with the variable, we need to enclose it within the curly braces so that the interpolation takes place with the entire string. It makes our code look cleaner and more concise. That is all for this lesson. In the next lesson we will be learning about the ranges and the double dot operators in Kotlin. Till then, stay tuned, happy coding, and keep smiling.

Back to the top