ELEVATE YOUR BUSINESS WITH

Limitless customization options & Elementor compatibility let anyone create a beautiful website with Valiance.

Strings in Kotlin

SELECT * FROM `itio_tutorial_master` WHERE `tutorial_menu`='25' AND `tutorial_submenu`='76' AND `tutorial_status`=1 LIMIT 1

Strings in Kotlin

📌 Strings in Kotlin

In Kotlin, a String is a sequence of characters enclosed within double quotes (" "). Strings are immutable, meaning their contents cannot be changed once created. Kotlin provides various functions and operations to manipulate and handle strings efficiently.


✅ Creating Strings

📌 1. String Declaration

kotlin

val greeting: String = "Hello, Kotlin!"println(greeting) // Output: Hello, Kotlin!

📌 2. Multiline String (Raw String)

Use triple quotes """ """ to declare a multiline string.

kotlin

val multilineText = val name = "Vikash"val age = 30println("My name is $name and I am years old.")

Output:

pgsql

My nameis Vikash and I am 30 years old.

  • For expressions, use ${}:

kotlin

val a = 5val b = 10println("The sum of $a and is ${a + b}")

Output:

python

The sum of 5and10is15


✅ String Comparison in Kotlin

📌 1. Using == for Structural Comparison

  • Compares the content of two strings.

kotlin

val str1 = "Kotlin"val str2 = "Kotlin"println(str1 == str2) // Output: true

📌 2. Using === for Referential Comparison

  • Compares memory addresses (checks if both refer to the same object).

kotlin

val str3 = "Kotlin"val str4 = str3println(str3 === str4) // Output: true


✅ String Iteration

You can iterate over the characters of a string using a for loop:

kotlin

val str = "Hello"for (char in str) { println(char)}

Output:

nginx

Hello


✅ Convert String to Other Types

You can easily convert a string to other data types using functions like:

ConversionFunctionExampleOutput
String to InttoInt()"123".toInt()123
String to DoubletoDouble()"123.45".toDouble()123.45
String to BooleantoBoolean()"true".toBoolean()true
Int to StringtoString()123.toString()"123"


✅ Conclusion

  • Strings are immutable in Kotlin.

  • Use string interpolation for cleaner code.

  • Perform string operations using various built-in functions.

  • Compare strings using == and ===.

  • Convert between data types easily using type conversion functions.

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql