Introduction
String handling helps us work with words and sentences in programs. It lets us read, change, and use text easily.
String handling helps us work with words and sentences in programs. It lets us read, change, and use text easily.
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.
$ to make a greeting message.val greeting = "Hello" val name = "Anna" val message = "$greeting, $name!" println(message)
val text = "Kotlin" println(text.length) println(text.lowercase())
val phrase = "I love coding" if (phrase.contains("love")) { println("Found the word love!") }
This program greets the user by name and checks if the entered password matches the saved one.
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") } }
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.
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.
val greeting = "Hello" val name = "Anna" println(greeting + ", " + name + "!")
val userName = 'John'
println("User: " + userName)val name = "Sam" val age = 25 val message = ??? println(message)
Pass the test and this pattern moves unknown