toBooleanStrictOrNull

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.

Since Kotlin

1.5

Samples

import samples.*
import java.util.Locale
import kotlin.test.*
fun main() { 
   //sampleStart 
   assertPrints("true".toBooleanStrictOrNull(), "true")
assertPrints("True".toBooleanStrictOrNull(), "null")
assertPrints("TRUE".toBooleanStrictOrNull(), "null")

assertPrints("false".toBooleanStrictOrNull(), "false")
assertPrints("False".toBooleanStrictOrNull(), "null")
assertPrints("FALSE".toBooleanStrictOrNull(), "null")

assertPrints("abc".toBooleanStrictOrNull(), "null") 
   //sampleEnd
}