Kotlin String Basics

A String stores text. It is one of the most-used data types in any program. In Kotlin, strings are objects — they come with built-in functions that let you search, slice, transform, and compare text.

Creating Strings

val greeting = "Hello, World!"
val empty = ""                   // empty string
val name: String = "Carlos"     // explicit type

String Length

val word = "Kotlin"
println(word.length)   // 6

Accessing Characters

Strings are indexed starting at 0. The first character is at position 0, the second at position 1, and so on.


"Kotlin"
  │ │ │ │ │ │
  K o t l i n
  0 1 2 3 4 5   ← index positions
val word = "Kotlin"
println(word[0])          // K
println(word[5])          // n
println(word.first())     // K
println(word.last())      // n

Common String Functions

val text = "  Hello Kotlin  "

println(text.trim())           // "Hello Kotlin" (removes spaces)
println(text.uppercase())      // "  HELLO KOTLIN  "
println(text.lowercase())      // "  hello kotlin  "
println(text.trimStart())      // "Hello Kotlin  " (only left)
println(text.trimEnd())        // "  Hello Kotlin" (only right)

Checking String Contents

val email = "user@example.com"

println(email.contains("@"))        // true
println(email.startsWith("user"))   // true
println(email.endsWith(".com"))      // true
println(email.isEmpty())            // false
println("".isEmpty())               // true
println("   ".isBlank())           // true (only spaces = blank)

Substring: Cutting a Piece of a String

val sentence = "Kotlin is powerful"

println(sentence.substring(7))        // "is powerful"
println(sentence.substring(0, 6))     // "Kotlin"
println(sentence.take(6))             // "Kotlin" (first 6 chars)
println(sentence.takeLast(8))         // "powerful"
println(sentence.drop(7))             // "is powerful"

Replacing Text in Strings

val original = "I love Java"
val updated = original.replace("Java", "Kotlin")
println(updated)   // I love Kotlin

Splitting a String

val csv = "apple,banana,cherry"
val fruits = csv.split(",")

println(fruits)         // [apple, banana, cherry]
println(fruits[0])      // apple
println(fruits[1])      // banana

Multiline Strings

Kotlin supports raw strings using triple quotes """. These preserve line breaks and do not need escape characters.

val address = """
    123 Main Street
    Springfield
    USA
""".trimIndent()

println(address)

Output:

123 Main Street
Springfield
USA

Escape Characters


Escape   │ Meaning
─────────┼──────────────────────
\n       │ New line
\t       │ Tab
\\       │ Backslash
\"       │ Double quote inside a string
\$       │ Dollar sign (avoids template interpretation)
println("Line 1\nLine 2")       // prints on two lines
println("Column1\tColumn2")     // tab between columns
println("Price: \$99")          // Price: $99

Comparing Strings

val a = "hello"
val b = "hello"
val c = "Hello"

println(a == b)                        // true (same content)
println(a == c)                        // false (different case)
println(a.equals(c, ignoreCase = true)) // true (case ignored)

Converting to and from String

val number = 42
val asString = number.toString()   // "42"

val text = "3.14"
val asDouble = text.toDouble()     // 3.14

val flag = true
val flagText = flag.toString()     // "true"

Leave a Comment

Your email address will not be published. Required fields are marked *