CHAPTER 11
Beginner
Strings in Kotlin
Updated: May 18, 2026
5 min read
# CHAPTER 11
Strings in Kotlin
1. Chapter Introduction
Strings represent sequences of characters (text). Whether you are displaying an article to a user, parsing JSON from a server, or validating a password input, you are working with Strings. Kotlin provides a rich set of built-in methods to manipulate, format, and search text. In this chapter, we will explore advanced String templates, essential String operations, and Raw Strings (Triple-quoted strings).2. Learning Objectives
By the end of this chapter, you will be able to:- Access individual characters in a String.
- Use built-in String methods (length, uppercase, substring).
- Compare strings securely.
- Create multi-line "Raw Strings" using triple quotes.
3. Strings are Immutable
Just like in Java, Strings in Kotlin are Immutable. Once a String object is created in memory, its contents can never be changed. Operations that appear to modify a String (like.uppercase()) actually create and return a *brand new String object*.
4. Basic String Operations
You can treat a String almost like an Array of characters.
kotlin
5. Essential String Methods
Kotlin provides dozens of helper methods to process text.
kotlin
6. String Comparison
To check if two strings contain the exact same text, use==.
To ignore uppercase/lowercase differences, use .equals(..., ignoreCase = true).
kotlin
7. Raw Strings (Triple Quotes)
If you need to print a massive block of text, formatting it with\n (newlines) and \" (escaped quotes) is a nightmare.
Kotlin introduces Raw Strings using three double quotes """. Raw strings can span multiple lines and do not require escaping characters!
kotlin
8. Common Mistakes
-
IndexOutOfBounds: Remember that Strings are 0-indexed. If a string has a length of 5, the maximum index is 4. Trying to access
str[5]will crash the program.
-
Forgetting Reassignment: Because Strings are immutable, writing
text.trim()does nothing to the original variable. You must assign the result to a new variable:val cleaned = text.trim().
9. Best Practices
-
Use
.trimIndent(): Whenever you use a multiline Raw String ("""), append.trimIndent(). This allows you to indent the string in your code to make it look pretty, without actually printing those huge blocks of blank space to the console.
10. Exercises
-
1.
Create a variable
password = " P@ssw0rd ".
-
2.
Create a new variable
cleanPassby trimming the whitespace.
-
3.
Check if
cleanPasscontains the@symbol and print the boolean result.
-
4.
Print the
cleanPassin all uppercase.
11. MCQs with Answers
Question 1
Are Strings mutable or immutable in Kotlin?
Question 2
How do you get the number of characters in a String?
Question 3
How do you access the first letter of a String variable named text?
Question 4
Which method removes leading and trailing whitespace from a String?
Question 5
How do you check if two strings contain the exact same characters?
Question 6
If you want to compare two strings but ignore capital letters, what should you use?
Question 7
What syntax is used to create a multi-line "Raw String"?
Question 8
What does .trimIndent() do to a Raw String?
Question 9
If val s = "Kotlin", what does s.substring(0, 3) return?
$) work inside Raw Strings (""")?
a) Yes b) No
Answer: a) Yes.
12. Interview Questions
- Q: What does it mean that Strings are "Immutable"? Why is this good for memory? (Answer: Once created, they cannot be changed. This allows the JVM to safely reuse string instances in a "String Pool" without worrying that one part of the app will secretly alter the string for another part).
-
Q: Explain how Kotlin handles String equality (
==) compared to Java.
13. Summary
Strings in Kotlin are robust and feature-rich. By leveraging methods like.trim(), .replace(), and .substring(), text processing becomes trivial. The addition of Triple-quoted Raw Strings makes embedding HTML, JSON, or SQL queries directly into Kotlin code incredibly clean and readable.