Kotlin Strings

Kotlin Strings

Strings are used for storing text. A string contains a collection of characters surrounded by double quotes.

var greeting = "Hello"

String Length and Functions

A String in Kotlin is an object, which means it contains properties and functions that can perform certain operations. For example, to find the length of a string, use the length property:

val txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
println("The length of the txt string is: " + txt.length)

String Templates / Interpolation

Kotlin features a fantastic way to insert variables directly inside a string without needing to use the + operator. You simply use the $ symbol followed by the variable name. This is known as String Templates.

String Template Example

fun main() {
  val firstName = "John"
  val lastName = "Doe"
  println("My name is $firstName $lastName")
}