If you write Android apps in Kotlin, you deal with strings on every screen. Kotlin handles strings much like Java but adds conveniences that cut down on boilerplate. This walk-through covers the essentials.
Strings in Kotlin are objects of the String class and are immutable. Once created, you cannot change the characters; any "modification" actually creates a new string in the string pool.
At a glance
- Declared with double quotes; char literals use single quotes.
- Access characters via str[index] or str.get(index).
- Dollar-sign templates embed variables and expressions.
- Two equality operators: === (reference) and == (structural).
Declaring and creating strings
In Kotlin, you declare a string with double quotes. Single quotes are reserved for single characters, so writing 'A' as a String throws a compile error. Assigning a number to a String variable also fails.
You can create an empty string with val s = "" or var s = String(). Concatenation uses the + operator, but the result is a new string object, not a mutation of the original.
Because strings are immutable, code like str += " more" does not append. It reassigns str to point at a new combined string.
Length, indexing, and slicing
Every string exposes its length as a property, accessed with str.length (no parentheses needed).
To grab a single character, Kotlin supports two equivalent forms: str[0] using the index operator, or str.get(4) using the function. Both return a Char. Out-of-range access throws a StringIndexOutOfBoundsException.
For substrings, use subSequence(startIndex, endIndex). It returns the chunk between the two indices but excludes the character at endIndex—a half-open range familiar from Java.
Comparing strings
Kotlin gives you two operators.
The === operator checks reference equality. Two variables are === only if they point at the exact same object in memory. The == operator checks structural equality, meaning the contents match.
For string checks, == is what you almost always want. Use === only when you need to know whether two variables share the same backing object—relevant when debugging string-pool behavior or comparing interned constants.
Negation uses !== and != respectively.
String templates and escape characters
String templates are where Kotlin earns its keep. Instead of concatenation, embed a variable with a $ prefix. For expressions, wrap in ${…}. Both compile to efficient StringBuilder calls.
Common escape sequences: \n (newline), \r (carriage return), \t (tab), \b (backspace), \" and \' (quotes), \\ (backslash), and \$ when you need a literal dollar sign instead of a template marker.
Quick decision checklist
| Scenario | Use this |
|---|---|
| Declare a string | val s = "" |
| Declare an empty string object | val s = String() |
| Access one character | str[index] |
| Get a substring | str.subSequence(start, end) |
| Check content equality | == |
| Check same object in memory | === |
| Embed a variable | "Hello, $name" |
| Embed an expression | "Items: ${items.size}" |
| Print a literal $ | "\$" |
Why this matters for Android developers
Strings appear everywhere—labels, network payloads, user input, formatted display text. Kotlin templates eliminate the clunky concatenation that makes Java code harder to read. The two equality operators catch many developers off guard; understanding the difference prevents subtle bugs when comparing strings from different sources.
The practical habit worth picking up early: prefer == over === for any string check, and prefer the index operator (str[i]) over str.get(i) when you can. Less noise, same result.
What to watch next
Kotlin strings are simple on the surface with small traps underneath. Get comfortable with immutability, templates, and the two equality operators, and you will avoid most string-related bugs.
Next, look into Kotlin's raw strings (triple-quoted) for multi-line text, and trimMargin for stripping leading whitespace. Both are useful for embedding JSON, SQL, or formatted text without escape-character noise.
Key takeaways
- Kotlin strings are immutable; "modifications" create new string objects.
- Use str[index] or str.get(index) to read individual characters.
- Prefer == for content checks; reserve === for reference identity.
- Dollar-sign templates replace concatenation and improve readability.
- Escape $ with a backslash when you need a literal dollar sign.
Frequently asked questions
Are Kotlin strings mutable?
No. Kotlin strings are immutable, the same as in Java. Any operation that looks like a change actually returns a new string.
How do I access a single character in a Kotlin string?
Use str[index] or str.get(index). Both return a Char and throw an exception if the index is out of bounds.
What is the difference between == and === for strings?
== compares content (structural equality), while === checks whether two variables point to the exact same object (reference equality). For most code, == is the right choice.
How do I include a variable in a string?
Use a string template. Prefix the variable with $, like "Hello, $name". For expressions, wrap in curly braces, like "Total: ${items.size}".
How do I print a literal dollar sign in a Kotlin string?
Escape it with a backslash, like "\$5.00", otherwise Kotlin will treat it as the start of a template.