Kotlin Strings

Kotlin Tutorial

Kotlin Strings – Complete Beginner Guide With Examples

Strings are one of the most commonly used data types in any programming language. In Kotlin, strings are powerful, flexible, and easy to work with.

Whether you’re building Android apps, backend services, or learning Kotlin for the first time, understanding how strings work is essential.

In this SEO-optimized, beginner-friendly guide, you will learn:

  • What a string is in Kotlin

  • How to declare and initialize strings

  • String templates (interpolation)

  • Common string functions

  • Multiline strings

  • String comparison

  • String immutability

  • Real-world examples

  • Common beginner mistakes

  • Best practices

Let’s get started


What Is a String in Kotlin?

A string is a sequence of characters used to store text.

In Kotlin, strings are represented by the String class.

Example:

Here:

  • "Kotlin" is a string literal

  • name stores the text

Strings can contain:

  • Letters

  • Numbers

  • Symbols

  • Spaces


How to Declare a String in Kotlin

There are two main ways to declare strings:


 Using val (Immutable String)

The value cannot be changed.


 Using var (Mutable Reference)

The variable reference can change, but the string itself remains immutable.


String Immutability in Kotlin

Strings in Kotlin are immutable.

This means:

  • Once created, a string cannot be changed.

  • Any modification creates a new string.

Example:

text remains unchanged.


String Templates in Kotlin (Interpolation)

Kotlin provides string templates to insert variables inside strings.


Basic Template

Output:

Hello Sanjit

Expression Template

Output:

Sum is 15

This makes string formatting easier.


Multiline Strings in Kotlin

Kotlin supports triple-quoted strings.


Example of Multiline String


 

Triple quotes allow:

  • Line breaks

  • Special characters

  • No escape sequences


Escape Characters in Strings

Some characters require escape symbols.

EscapeMeaning
\nNew line
\tTab
Double quote
\Backslash

Example:


Common String Functions in Kotlin

Kotlin provides many built-in string functions.


 length


 uppercase() and lowercase()


 contains()


 substring()


 replace()


 split()


String Comparison in Kotlin

Kotlin provides two types of equality checks.


Structural Equality (==)

Checks content equality.


 


Referential Equality (===)

Checks memory reference.


Real-World Example – Login Validation


 

Strings are heavily used in authentication systems.


Checking Empty or Blank Strings


isEmpty()


isBlank()

Difference:

  • isEmpty() → checks length

  • isBlank() → checks spaces


String to Number Conversion


Convert String to Int


Safe Conversion

Avoids crashes.


String Formatting in Kotlin

Using String.format():


Iterating Over Characters


 


StringBuilder in Kotlin

For heavy string operations, use StringBuilder.

Better performance for multiple concatenations.


Common Beginner Mistakes

 Using === Instead of ==

Use == for content comparison.


Ignoring Null Safety

Use safe calls for nullable strings.


 Overusing + in Large Loops

Use StringBuilder instead.


Performance Considerations

  • Strings are immutable

  • Frequent concatenation creates new objects

  • Use StringBuilder for better performance


When Should You Use Strings?

Use strings when:

  • Storing user input

  • Handling text data

  • Building UI content

  • Working with APIs

  • Logging

Strings are everywhere in Kotlin applications.


Frequently Asked Questions (FAQs)

1. What is a string in Kotlin?

A string in Kotlin is a sequence of characters used to represent text.

2. Are strings mutable in Kotlin?

No, strings are immutable.

3. What is string template?

String template allows embedding variables inside strings using $.

4. What is the difference between == and === in strings?

== checks content equality, === checks reference equality.

5. How do I create multiline strings in Kotlin?

Use triple quotes “”” “””.


Conclusion

Kotlin String are powerful and easy to use.

You learned:

  • String declaration

  • Templates

  • Multiline strings

  • Built-in functions

  • Comparison

  • Performance tips

Mastering strings is essential for every Kotlin developer.

You may also like...