Kotlin Tutorial
Estimated reading: 3 minutes 28 views

🔤 Kotlin Strings & Booleans – Formatting, Templates, and Logical Operations

🧲 Introduction – Manage Text and Logic with Kotlin’s String & Boolean Types

In Kotlin, Strings are used for text manipulation and representation, while Booleans control logical decisions and flow. Together, they form the foundation of interactive, conditional, and user-facing programs.

Kotlin provides powerful string handling features like template expressions, multi-line strings, and built-in methods, along with concise Boolean logic for comparisons and conditions.

🎯 In this guide, you’ll learn:

  • How to declare and use strings with template expressions
  • How to manipulate strings using Kotlin’s built-in methods
  • How to use Booleans for conditional logic and decision-making
  • How to apply logical operators like &&, ||, and !

📘 Topics Covered

🔍 Topic📖 Description
🧵 Kotlin – StringsCreating, formatting, and manipulating string values using templates and functions.
Kotlin – BooleansPerforming true/false operations with logical operators and comparisons.

🔍 Detailed Sections & Examples

🧵 Kotlin – Strings

📌 Declaring Strings

val message: String = "Welcome to Kotlin!"
val name = "Vaibhav"

Kotlin infers the type if not specified explicitly.


💡 String Templates

val age = 28
val greeting = "Hello, my age is $age"
println(greeting)

📌 Use $variable or ${expression} inside double-quoted strings.

val score = 85
println("Score doubled is ${score * 2}")  // Output: Score doubled is 170

📜 Multi-line Strings

val bio = """
    Kotlin is modern.
    Kotlin is powerful.
    Kotlin is fun!
""".trimIndent()

println(bio)

📌 Use triple quotes """ for multi-line strings. trimIndent() removes unnecessary indentation.


🔧 Common String Methods

val title = "Kotlin Programming"
println(title.uppercase())     // KOTLIN PROGRAMMING
println(title.lowercase())     // kotlin programming
println(title.length)          // 18
println(title.contains("gram")) // true

✅ Kotlin – Booleans

📌 Boolean Declaration

val isActive: Boolean = true
val isLoggedIn = false

🔍 Logical Operations

val x = 10
val y = 20

val result1 = x < y && y > 15  // true
val result2 = x > y || y == 20 // true
val result3 = !(x == y)        // true
OperatorDescription
&&Logical AND
`
!Logical NOT

📋 Comparisons

val a = 5
val b = 10

println(a == b)  // false
println(a != b)  // true
println(a <= b)  // true

Kotlin supports all standard comparison operators: ==, !=, <, >, <=, >=.


📌 Summary – Recap & Next Steps

Strings and Booleans are core building blocks in Kotlin, used for displaying messages, user input, control structures, and business logic. Mastering these types helps you write interactive and dynamic programs effectively.

🔍 Key Takeaways:

  • Kotlin strings support interpolation, multiline text, and rich methods.
  • Booleans control logic with comparisons and operators like &&, ||, and !.
  • Use expressions inside string templates for dynamic output.

⚙️ Practical Use Cases:

  • Generating dynamic UI text in Android apps
  • Writing condition-based flow in game logic or forms
  • Parsing user input, logs, or reports

❓ Frequently Asked Questions

How do I embed variables in Kotlin strings?
✅ Use $varName or ${expression} inside a double-quoted string.

val name = "Kotlin"
println("Welcome to $name!")

What’s the difference between == and === in Kotlin?
== checks structural equality (content), while === checks reference equality (same object in memory).


Can I compare two strings in Kotlin?
✅ Yes. Use ==, compareTo(), or equals().

val a = "Kotlin"
val b = "Kotlin"
println(a == b)  // true

How do I reverse a string in Kotlin?
✅ Use reversed():

val word = "Kotlin"
println(word.reversed())  // niltoK

Can Booleans be inferred from expressions?
✅ Yes. Kotlin infers Boolean results from comparisons:

val isValid = 5 < 10  // true

Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Kotlin Strings & Booleans

Or Copy Link

CONTENTS
Scroll to Top