toBooleanStrict
Требования к платформе и версии: JVM (1.5), JS (1.5), Native (1.5)
fun String.toBooleanStrict(): Boolean
Возвращает true, если содержимое этой строки равно слову "true", false, если оно равно "false", и выбрасывает исключение в противном случае.
Также доступна более лояльная версия функции для nullable String, String?.toBoolean. Обратите внимание, что эта функция чувствительна к регистру.
import java.util.Locale
import kotlin.test.*
fun main(args: Array<String>) {
//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–2022 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.html