- Kotlin - Home
- Kotlin - Overview
- Kotlin - Environment Setup
- Kotlin - Architecture
- Kotlin - Basic Syntax
- Kotlin - Comments
- Kotlin - Keywords
- Kotlin - Variables
- Kotlin - Data Types
- Kotlin - Operators
- Kotlin - Booleans
- Kotlin - Strings
- Kotlin - Arrays
- Kotlin - Ranges
- Kotlin - Functions
- Kotlin Control Flow
- Kotlin - Control Flow
- Kotlin - if...Else Expression
- Kotlin - When Expression
- Kotlin - For Loop
- Kotlin - While Loop
- Kotlin - Break and Continue
- Kotlin Collections
- Kotlin - Collections
- Kotlin - Lists
- Kotlin - Sets
- Kotlin - Maps
- Kotlin Objects and Classes
- Kotlin - Class and Objects
- Kotlin - Constructors
- Kotlin - Inheritance
- Kotlin - Abstract Classes
- Kotlin - Interface
- Kotlin - Visibility Control
- Kotlin - Extension
- Kotlin - Data Classes
- Kotlin - Sealed Class
- Kotlin - Generics
- Kotlin - Delegation
- Kotlin - Destructuring Declarations
- Kotlin - Exception Handling
Kotlin String - toBoolean() Function
The Kotlin string toBoolean() function is used to convert or parse a string into a Boolean value and returns true if the string is true and false for any other value, including null (when the string is empty, or any other string that is not "true").
Syntax
Following is the syntax of the Kotlin string toBoolean() function −
fun String.toBooleanStrict(): Boolean
Parameters
This function does not accepts any parameters.
Return value
This function returns a Boolean value: "true' if the string is "true" (case insensitive); otherwise, false.
Example 1
Following is the basic example of the toBoolean() function: If string is "true" −
fun main() {
// if string is true
val str = "true"
val result = str.toBoolean()
println(result)
}
Output
If the string is true this function return true −
true
Example 2
Let's see another example of the toBoolean() function. Here we are using a string with mixed case −
fun main() {
// if string is True
val str = "TrUe"
val result = str.toBoolean()
println(result)
}
Output
If the string (TrUe) is case-sensitive this function return true −
true
Example 3
Now, let's see another example of the toBoolean() if the string is "false" −
fun main() {
// if string is false
val str = "false"
val result = str.toBoolean()
println(result)
}
Output
If the string is "false" this function return false −
false