Kotlin Strings


Kotlin Strings – Your Text Toolbox

In Kotlin, strings are like little boxes full of characters wrapped in double quotes.

var message = "Hey there!"

Kotlin's smart brain detects it's a string just by the quotes. No need to shout out "String"—unless you want to.


Want to be explicit?

If you feel fancy, you can mention the type directly:

var welcome: String = "Hi!"

Declare now, assign later?

Sure! But only if you label the type from the start:

This is fine:

var name: String 
Name = "Alice" 

This throws an error:

var name
Name = "Alice" // Error: Kotlin doesn't know what type this is 

Accessing Letters in a String

Each letter in a string has a position number (starting from 0). Use square brackets to pick a letter:

val txt = "Kotlin" 
println(txt[0]) // K 
Println(txt[2]) // t 

Positions go like this:

0 = first, 1 = second, 2 = third… you get the idea.


String Size – Count the Letters

You can measure how long a string is using .length:

val alpha = "ABCDEF" 
Println(alpha.length) // 6 

Kotlin treats strings as objects, so you can call functions and properties on them!


String Power Tools (Functions)

Kotlin strings come with built-in helpers:

val word = "Hello World" 
println(word.toUpperCase()) // HELLO WORLD 
Println(word.toLowerCase()) // hello world 

Great for formatting, search, or clean-up.


Compare Two Strings

Want to check if two strings match? Use .compareTo():

val a = "Hello" 
val b = "Hello" 
Println(a.compareTo(b)) // 0 → same 

Returns 0 when they're equal.


Find Text Inside a String

Use .indexOf() to locate text:

val line = "Find where this word starts" 
Println(line.indexOf("this")) // Outputs 10 

Starts counting from 0, just like everything else in Kotlin.


Using Quotes Inside Quotes

Want ' in a string? No problem:

val one = "It's awesome" 
Val two = "That's amazing" 

Kotlin handles single quotes inside double quotes just fine.


Merging Text – String Join

With Plus Sign:

val first = "Jane" 
val last = "Doe" 
Println(first + " " + last) 

With .plus() method:

println(first.plus(last)) // Same output 

String Templates – Insert Variables Easily

This is Kotlin's superpower! Use $ to plug variables directly:

val user = "Sahand" 
val role = "Developer" 
Println("Hi, I’m $user, a $role") 

You can also add expressions:

val age = 30 
Println("Next year, I’ll be ${age + 1}") 

No messy + symbols. Just clean, readable strings!


Summary Table – String Essentials

Feature Kotlin Code Example Notes
Declare var text = "Hello" Kotlin guesses type
Set Later var name: String then assign Must define type up front
Access Letter text[1] Position starts at 0
Length text.length Total characters
Upper/Lower text.toUpperCase() Formatting help
Compare a.compareTo(b) Returns 0 if equal
Find text.indexOf("word") First match's position
Add Strings a + b or a.plus(b) Merges two
Template "Name is $name" Easy variable insertion

Prefer Learning by Watching?

Watch these YouTube tutorials to understand KOTLIN Tutorial visually:

What You'll Learn:
  • 📌 Kotlin Tutorial for Beginners - Kotlin String Function (With Example)
  • 📌 #10 Kotlin Tutorial | String Template
Previous Next