0
0
Kotlinprogramming~5 mins

Why string handling matters in Kotlin

Choose your learning style9 modes available
Introduction

String handling helps us work with words and sentences in programs. It lets us read, change, and use text easily.

When you want to greet a user by name in an app.
When you need to check if a password is correct.
When you want to show messages or instructions on the screen.
When you need to save or load text data like names or addresses.
When you want to search for a word inside a sentence.
Syntax
Kotlin
val text: String = "Hello, World!"

// Common string operations:
text.length          // Get number of characters
text.uppercase()   // Convert to uppercase
text.contains("World") // Check if text has a word
text.substring(0, 5) // Get part of the text

Strings in Kotlin are surrounded by double quotes.

You can use built-in functions to work with strings easily.

Examples
This joins two strings using $ to make a greeting message.
Kotlin
val greeting = "Hello"
val name = "Anna"
val message = "$greeting, $name!"
println(message)
Shows how to get the length and convert to lowercase.
Kotlin
val text = "Kotlin"
println(text.length)
println(text.lowercase())
Checks if a word is inside a string and prints a message.
Kotlin
val phrase = "I love coding"
if (phrase.contains("love")) {
    println("Found the word love!")
}
Sample Program

This program greets the user by name and checks if the entered password matches the saved one.

Kotlin
fun main() {
    val userName = "Sam"
    val welcomeMessage = "Welcome, $userName!"
    println(welcomeMessage)

    val password = "abc123"
    val input = "abc123"
    if (input == password) {
        println("Access granted")
    } else {
        println("Access denied")
    }
}
OutputSuccess
Important Notes

Strings are very common in programs because we use text all the time.

Learning to handle strings well helps you make your programs more useful and friendly.

Summary

Strings let us work with text in programs.

We use string handling to show messages, check input, and combine words.

Kotlin has many easy tools to work with strings.