Example of Kotlin enum with string values:
Enum is class in Kotlin and we can use it as a group of constant values. We can use enum as a type of a variable. The enum keyword is used to define a enum class. The below example shows how to create an enum:
enum class Days {
SUN, MON, TUE, WED, THU, FRI, SAT
}
We created an enum class Days. All of the comma separated values are called enum constants. Note that each of these enum constants are objects.
enum class Days {
SUN, MON, TUE, THU, FRI, SAT
}
fun main() {
println(Days.SUN)
}
Here, we are printing the value of Days.SUN. If you run this program, it will print SUN.
How to use constructor in Enum classes:
The enum constants are final variables and we can’t modify these in runtime. But, we can initialize these with a value while initializing the enum class i.e. we can use eum constructors. Let’s take a look at the below example:
enum class Days(val value: Int) {
SUN(0), MON(1), TUE(2), THU(3), FRI(4), SAT(5)
}
fun main() {
println(Days.SUN)
println(Days.SUN.value)
}
In this example, we are initializing the enum values of the Days class by using a constructor and we are printing the values of SUN and its value.
If you run this program, it will print the below output:
SUN
0
Method 1: How to create enum from string values or how to search for enum by a string value:
Suppose you got a string SUN and you want to instantiate a Days.SUN enum from that value or in simple words, you want to get an enum by string value.
Kotlin provides a method called valueOf that we can use to get an enum constant by string value. We can pass a string to this method and it will return the enum value that matched the string value we passed.
Note that this method will throw an IllegalArgumentException if it doesn’t find any enum constant with the value we passed.
This method is defined as like below:
EnumClass.valueOf(value: String): EnumClass
Let’s take a look at the below example:
enum class Days(val value: Int) {
SUN(0), MON(1), TUE(2), THU(3), FRI(4), SAT(5)
}
fun main() {
val sat = Days.valueOf("SAT")
println(sat.value)
}
In this example, we called valueOf method to the Days enum class and passed SAT. It will return Days.SAT. Hence, the last print statement will print 5.
Method 2: Generic way by using enumValues():
If you don’t want to use the valueOf method, you can also use the generic method enumValues
enum class Days(val value: Int) {
SUN(0), MON(1), TUE(2), THU(3), FRI(4), SAT(5)
}
fun main() {
val sat = enumValueOf<Days>("SAT")
println(sat.value)
}
It will print the same output.
Similar to valueOf, this method also throws IllegalArgumentException if no enum object is found by that string name.
How to handle the exception:
The valueOf and enumValueOf method throws IllegalArgumentException if it doesn’t find the string in the enum class. You can use try-catch to handle any unwanted exceptions:
enum class Days(val value: Int) {
SUN(0), MON(1), TUE(2), THU(3), FRI(4), SAT(5)
}
fun main() {
try {
val sat = enumValueOf<Days>("Hello")
println(sat.value)
} catch (e: IllegalArgumentException) {
println("IllegalArgumentException on enumValueOf")
}
}
Output:
IllegalArgumentException on enumValueOf
You might also like:
- How to remove the first n characters from a string in Kotlin
- Check if a string starts with a character or sub-string in Kotlin
- Check if a string ends with another string or character in Kotlin
- How to compare two strings in Kotlin in different ways
- 5 different examples to split a string in Kotlin
- How to convert a string to byte array in Kotlin
- 3 different ways to convert a byte array to string in Kotlin
- Introduction to Kotlin Interpolation with examples