Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A String in Kotlin represents an immutable sequence of UTF-16 characters. On the JVM, Kotlin’s String class is mapped directly to java.lang.String. Because strings are strictly immutable, any operation that appears to modify a string (such as concatenation or replacement) allocates and returns a new String instance rather than mutating the original object in memory. Kotlin provides two primary types of string literals: escaped strings and raw strings. Escaped Strings Enclosed in double quotes ("), escaped strings support standard C-style character escaping mechanisms (e.g., \n, \t, \", \\).
val escapedString: String = "First line\nSecond line"
Raw Strings Enclosed in triple quotes ("""), raw strings do not support character escaping. They can span multiple lines and contain arbitrary text, including unescaped double quotes. They are typically paired with standard library functions like trimIndent() or trimMargin() to strip leading whitespace for code readability.
val rawString: String = """
    |SELECT * FROM users
    |WHERE age > 18
""".trimMargin()
String Templates (Interpolation) Kotlin evaluates embedded expressions within both escaped and raw string literals using the $ character.
  • Simple variable references use a bare $.
  • Complex expressions or property accesses require curly braces ${}.
val count = 5
val simpleTemplate = "Total items: $count"
val expressionTemplate = "Doubled: ${count * 2}"
val propertyAccess = "Length: ${simpleTemplate.length}"
Properties and Indexing String implements the CharSequence interface. Individual characters can be accessed in constant time via the indexing operator [], which internally invokes the get(index: Int) function.
val text = "Kotlin"
val firstChar: Char = text[0]       // 'K'
val lastChar: Char = text.last()    // 'n'
val totalLength: Int = text.length  // 6
Iteration Because String is a sequence of Char elements, it supports direct iteration via standard loops or higher-order functions.
for (char in "Kotlin") {
    // Iterates through 'K', 'o', 't', 'l', 'i', 'n'
}
Equality Kotlin enforces a strict distinction between structural and referential equality, which applies directly to strings:
  • Structural Equality (==): Evaluates whether the character sequences of two strings are identical. It safely handles nulls and compiles down to java.lang.String.equals() on the JVM.
  • Referential Equality (===): Evaluates whether two references point to the exact same object in memory. Due to JVM string pooling, identical string literals often share the same reference.
val str1 = "Kotlin"
val str2 = "Kot" + "lin"
val str3 = String(charArrayOf('K', 'o', 't', 'l', 'i', 'n'))

val isStructurallyEqual = (str1 == str2)   // true
val isReferentiallyEqual = (str1 === str3) // false (different memory allocations)
Master Kotlin with Deep Grasping Methodology!Learn More