toBooleanStrictOrNull

Platform and version requirements: JVM (1.5), JS (1.5), Native (1.5)
fun String.toBooleanStrictOrNull(): Boolean?

Returns true if the content of this string is equal to the word "true", false if it is equal to "false", and null otherwise.

There is also a lenient version of the function available on nullable String, String?.toBoolean. Note that this function is case-sensitive.

import java.util.Locale
import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
println("true".toBooleanStrictOrNull()) // true
println("True".toBooleanStrictOrNull()) // null
println("TRUE".toBooleanStrictOrNull()) // null

println("false".toBooleanStrictOrNull()) // false
println("False".toBooleanStrictOrNull()) // null
println("FALSE".toBooleanStrictOrNull()) // null

println("abc".toBooleanStrictOrNull()) // null
//sampleEnd
}

© 2010–2021 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/to-boolean-strict-or-null.html