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