String Concatenation and Interpolation

Tim Buchalka
A free video tutorial from Tim Buchalka
Java Python Android and C# Expert Developer - 974K+ students
4.5 instructor rating • 12 courses • 974,815 students

Learn more from the full course

Android App Development Masterclass using Kotlin

Learn Kotlin Android App Development And Become an Android Developer. Incl. Kotlin Tutorial and Android Tutorial Videos

61:50:31 of on-demand video • Updated April 2021

  • Learn the core Android app development and Kotlin skills to build real Android apps.
  • Learn how to target current and older versions of Android when writing your apps.
  • Understand how to create Android apps using Kotlin.
  • Be able to apply for Android app development roles.
English In the previous video we looked at how to perform arithmetic on numeric variables. So we saw the basic operations of addition subtraction multiplication and division we also discussed the int and double types and why we need to use doubles when working with fractional numbers in this video let's look at another thing we can do with strings. Now we saw that tow we cannot perform arithmetic on them we can't multiply them for example but we can do something that looks like addition, so open up the Kotlin tutorial project if you haven't already we're going to start making some changes so what I'm going to do is modify all our print line statements in a moment so I'm going to keep our existing code and I'm going to add this to the end let's come down here and basically after the last our line 26 and we worked on with the printing and years I'm going to come down here and type in print line on line 28, parentheses my name in double quotes is plus Tim. Now when dealing with strings the plus symbol doesn't try to add the strings in a mathematical sense instead it joins them together and that's called concatenating the strings which is just a posh word for joining so when we run the program that's actually yeah give it a go you can see down here we get the message my name is Tim Buchalka. Now another thing you can do is add numeric values to a string and that may seem odd but it's so common to print numbers out with some text describing what they are that Kotlin allows you to use a number, it then concatenates the number onto the end of the string then that lets us make the output a bit more meaningful by adding some text to the output so I'm going to modify the code now that prints out the answer of how many years 130 weeks is, this is the code on line 26 so we can actually change that and put double quotes hundred 30 weeks in years is colon another type of quote then plus years. So this makes it more obvious what that value 2.5 means. Now we've hard-coded the value 130 in the string which means we'd have to make a change in both places if we had a different number of weeks for example if we want to calculate how many weeks children 34 weeks is, we could change this to 234 but when we run it this gives the correct answer 4.5 but the text isn't right. Now you might be tempted to add the number of weeks at the beginning but that won't work so in other words I can't come along here and put weeks plus something like that weeks plus and weeks being the value plus then the string and then the years but that doesn't work as you can see and that's because Kotlin doesn't allow us to concatenate on two numbers so because the first variable in the expression is a number it tries to perform an addition adding a string value to a number it doesn't work and we get the error and the error is none of the following functions can be called with the arguments supplied but that's not the end of the world because there's a much better way, in fact I suggest you don't concatenate strings like we've just done, so probably asking now why have I shown you how to do something that I don't recommend you do well this is the Kotlin for Android course but when dealing with Android you're going to see a lot of Java code. Now Java doesn't have the feature that I'm about to show you which means you'll see strings concatenated with the plus operator in Java. Now it's useful to be able to read code like that and understand what it's doing but there's no reason for you to write it that way in Kotlin, so I'm going to undo that last change so the program runs, so we're back to how it was and it was working before then I'm going to use something called string interpolation so I'm gonna put a dollar sign what you're going to do is delete the second double quote and put a dollar sign Tim and then close off the double quote so you can see we've got one string but wheezing a dollar sign Tim within the double quotes first you know that says make sure it works. My name is Tim Buchalka so that's working. Now string interpolation in Kotlin is just a fancy way of saying that Kotlin will replace variables with their values. Now to use a string interpolation a prefix the variable name with a dollar symbol like you've seen me here do on line 28, so dollar Tim tells Kotlin to use the value of the variable Tim rather than the 3 character variable name. Now we can actually use that on line 26 to include the number of weeks at the start of the string some of you use interpolation for the year as well so we can do that by coming over here instead of 130 I'm going to change it to dollar weeks like so and it also makes it easy to produce more readable output without using loads of pluses so let's just fix that string up in time so we've got dollar weeks weeks is go ahead in all the time and it should be so make that is then we're going to put dollar years I'm gonna delete the rest of it there and put a double quote on the end so I've got dollar weeks space week space is space dollar years space years all in double quotes as you can see and that reads much better if we actually run this to confirm that it works 234 weeks is 4.5 years and my name is Tim Burchett on the next line so whenever you need to include the value of a variable in a string you can use string interpolation by prefixing the variable name with a dollar symbol. Now if you need to include more complicated expressions you surround the expression with curly braces now you can see that working on that aligned 21 where we print out a quarter of our apples so coming down here - line 21 and I'm going to put double quotes there a quarter of the apples is then a dollar sign then a left curly brace and then a right curly-brace after the end of the expression then another double quote so it basically enclosed the entire expression in curly braces after the dollar symbol as you can see there on line 21 now if you run that, a quarter of the apples is one which is correct Apple was divided by four apples having the value of six and obviously we're getting the number rounded up because we're actually using integer division so there's no fractional component output. Alright so there's still three values we have no description so it's time for a challenge. Now throughout the course there's going to be challenges for you to try these are designed for you to check your understanding of the material we've covered and they'll get progressively more involved as we work through the course now they're not a test they're there for you to check that you've understood everything it's also not important that you get exactly the same solution as me the real test here is does your solution work if your solution to a challenge produce the correct result then you've completed the challenge successfully alright with that said let's have a look at our first challenge so for each of the remaining three valleys that are being printed without a description you string interpolation to display the valleys with some descriptive text. Now use whatever descriptive text you want the important thing is that you include the values in the text so try that out pause the video now and come back to this video on playing again when you're ready to see in more solution pause the video now. Okay so how did you got? Hopefully you managed to come up with a solution for that so let's actually have a go at string interpolation for those remaining prints the printouts and the first one is is change weekly salary so let's change that so I'm going to put double quotes I'm gonna put Tim's weekly salary is in a dollar sign before the Tim's weekly salary variable and then another double quote to close the line off that's the first one let's change the second line they are long 12 in double quotes monthly monthly that comes to and a dollar sign for Tim's monthly salary and in the double quote then the next one was down here printed fruit on line 19 this actually changed that we're going to put double quote we'll start with the apples dollar apples apples miners dollar oranges oranges please and then dollar fruit pieces and then s in parentheses in case the value is one or less of fruit enclosing double quote so delight apples apples boilers dollar oranges oranges leaves dollar fruit piece or pieces in parentheses of fruit so let's actually run that and make sure that it works Tims weekly salary is thirty two monthly that comes to one twenty eight and six apples take five oranges leaves one piece pieces of fruit so that's the challenge completed so essentially whenever we want to include a value in the output string we prefix the variable with the dollar sign. for the apples miners oranges have included that values for the apples and oranges as you can see there, as well as the number of fruit then it leaves just to show you that you can use as many variables as you need in a string when using string interpolation there the challenge only asked you to describe what the value of one represented if you haven't included all the variables that's fine as well. So if your solution worked and included texts with each of those values congratulations now one thing you might be wondering is how to use the dollar symbol in strings if it's actually used for interpolation well turns out Kotlin is quite clever in that respect if what follows the dollar sign isn't a very variable name or an expression inside curly braces, then the dollars just treated as an ordinary character so when printing out my weekly salary we come back here to have a look on line eleven we can actually change that to include the dollar sign for example so Tim's dollar space then weekly if we run that we get the example the result we'd expect Tim's dollar sign weekly salary so there's nothing special about the first dollar sign symbol and it just prints out. Now if we want to prefix the amount with a dollar that will also work as well so we can come up here to that line line 11 again and we could put a dollar sign before the other dollar sign and run that again Tim's dollar weekly salary and then we've got the dollar sign there showing before the number thirty-two now if for some reason we wanted to include a dollar a dollar sign followed by a variable name then we have to escape the dollar using a backslash. Now this is a bit contrived we're going to print the print dollar Tim on the line after printing my name just to show you how to do it so we'll do something like the print 'ln double quotes I can print and print backslash dollar sign Tim if we run that we're actually printing out the dollar Tim now as opposed to using string interpolation to print the value of the Tim variable there's probably not awful that you want to do that but that's how to do what if you do need to do it. Now there are a few other things we can deal with strings I'm going to be explaining them as we use them when writing the code for our Android apps. So I'm going to stop the video here, in the next video we're going to take a look at the warning ticks in the right margin and also why I've used well in some places and var in others, so I will see you in the next video.