to Boolean Strict
В Kotlin с: 1.5
fun String.toBooleanStrict(): Boolean
Возвращает true, если содержимое этой строки равно слову "true", false, если оно равно "false", и в противном случае выбрасывает исключение.
Также доступна версия функции с менее строгой проверкой для nullable String: String?.toBoolean. Обратите внимание, что эта функция учитывает регистр.
Примеры
import kotlin.test.*
fun main() {
//sampleStart
println("true".toBooleanStrict()) // true
// "True".toBooleanStrict() // will fail
// "TRUE".toBooleanStrict() // will fail
println("false".toBooleanStrict()) // false
// "False".toBooleanStrict() // will fail
// "FALSE".toBooleanStrict() // will fail
// "abc".toBooleanStrict() // will fail
//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.html