to Boolean Strict Or Null
Начиная с Kotlin: 1.5
fun String.toBooleanStrictOrNull(): Boolean?
Возвращает true, если содержимое этой строки равно слову "true", false, если оно равно "false", и null в противном случае.
Для nullable String также доступна версия функции, допускающая более свободное преобразование: String?.toBoolean. Обратите внимание, что эта функция учитывает регистр.
Примеры
import kotlin.test.*
fun main() {
//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–2026 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.text/to-boolean-strict-or-null.html