This article explores different ways to convert a string containing exactly one character to its equivalent character value in Kotlin.

1. Using first() function

The standard approach to converting a string to a char in Kotlin is to use the first() function, which returns the first index’s element.

Download Code

2. Using [] operator

Another approach is to use the indexing operator [] on the specified string to retrieve the character value present at the first index.

Download Code

3. Using single() function

A string can contain a single character, more than one character, or can be empty. If you need to return a single character and throw an exception if the char sequence is empty or has more than one character, use the single() function.

Download Code

That’s all about converting a string to a char in Kotlin.