Instruction 2

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Strings

Strings are arguably the most used data type in software programs. Strings represent text. In Kotlin, Strings store a sequence of characters which are declared in double quotes: "Hippopotamus". Since a string is a sequence, you can also inspect each character by iterating over it. Add the following code to your main function. You’ll print every character that forms part of the string as you loop over it:

fun main() {
   val animal = "Hippopotamus"

   for (character in animal) {
      println(character)
   }
}
H
i
p
p
o
p
o
t
a
m
u
s
val animal = "Hippopotamus"
animal = "Giraffe"
fun main() {
   val animal = "Hippopotamus"
   println(animal.uppercase()) // A new string is created and returned to the println() function
   println(animal) // The string remains the same
}
HIPPOPOTAMUS
Hippopotamus

Using String Concatenation

To create a string, you can use a method known as String concatenation. This is where you use the+operator or its equivalent `plus() function to edit a new value of a String.

fun main() {
   val fullName = "Hippo" + "potamus"
   println(fullName) // A new string is created from the two strings.
}
fun main() {
   val firstName = "Mountain"
   val lastName = "Tiger"
   val fullName = firstName + " " + lastName

   println(fullName) // A new string is created from the two variables and the string literal with a space
}

String Templates and Interpolation

Kotlin has a special feature that evaluates code within a String. After evaluation, Kotlin converts the result to a string if it isn’t one already. It then concatenates the evaluation result with the rest of the string. The result is added at the original location of the expression in the String literal. This convenient conversion reduces the code required to update and use a String.

fun main() {
   val isCorrect = true
   val tigerSpecies = 9
   val animal = "Tigers"

   println("That is $isCorrect. There are $tigerSpecies species of $animal") // Notice how different types of data are evaluated and converted into a String
}
That is true. There are 9 species of Tigers
fun main() {
   val tigerSpecies = 9
   val animal = "Tigers"

   println("That is ${tigerSpecies == 9}. There are $tigerSpecies species of $animal") // The expression in curly braces evaluates to `true`
}
fun main() {
   val amount = 100_000_000
   println("Gold is worth more than \$$amount")
}
fun main() {
   val amount = 25

   val chat = """
      Ann: How much does this book cost?
      Bryan: This book costs $${amount}.
      And you get a discount of ${'$'}9.99
   """

   println(chat)
}
      Ann: How much does this book cost?
      Bryan: This book costs $25.
      And you get a discount of $9.99
fun main() {
   val lowest = 45
   val cash = 200
   val books = 20

   val chat = """
      Ann: With $$cash how many books can I afford?
      Bryan: ${ if (cash > lowest) 20 else 10 } books.
      Ann: ${
          if (books > 10) "Awesome!"
          else "That's OK!"
         }
   """

println(chat)
}
      Ann: With $200 how many books can I afford?
      Bryan: 20 books.
      Ann: Awesome!
See forum comments
Download course materials from Github
Previous: Instruction 1 Next: Demo