Lookup enum by String value in Kotlin Program ?

  Kotlin Interview Q&A

In this article you will learn today how to convert string values to enum using the valueOf () method of an enum in Komalin.

Lookup enum by string value

enum class TextStyle {
    BOLD, ITALICS, UNDERLINE, STRIKETHROUGH
}

fun main(args: Array<String>) {

    val style = "Bold"

    val textStyle = TextStyle.valueOf(style.toUpperCase())

    println(textStyle)

}

Result

BOLD

LEAVE A COMMENT