1. Introduction
Sometimes, while processing user input or handling data validation, it’s necessary to convert a String to a Char. Note that in Kotlin, we surround strings with a double quotation mark (” “), while we use single quotation marks for a Char (‘ ‘).con
In this tutorial, we’ll discuss various ways to convert a string to a char in Kotlin with examples.
2. Definition
In Kotlin, a String and a Char are two distinct data types. A String is a sequence of characters with zero or more characters, while a Char represents a single Unicode character. Furthermore, the String class implements the CharSequence interface, which means that we can treat strings as sequences of characters.
To convert a String to a Char using the approaches in this article, we must ensure that the string is not empty before attempting the conversion, otherwise, we might run into some exceptions.
3. Using the get() Method
We can use the get() method to retrieve a char from a string when our string has at least one character. All we have to do is supply this method with the index of the character we wish to retrieve from the string:
@Test
fun `converts string to char using get method`(){
val str = "H"
val char = str.get(0)
assertEquals('H', char)
}
In this unit test, we convert a string “H” to a char by supplying the get method with the zero index position. Finally, we assert that the result is the character ‘H’.
Note that we can use this method to get any character from a string by supplying it with the index position.
4. Using the single() Method
Another way to convert a string to a char involves the use of Kotlin’s built-in single() method. It returns a single character or throws an exception if the string is empty or has more than one character. If the string contains more than one character, this method will throw an IllegalArgumentException. If the string is empty, it’ll throw a NoSuchElementException:
@Test
fun `converts string to char using single method`(){
val str = "H"
val char = str.single()
assertEquals('H', char)
}
5. Using the first() Method
Thirdly, we can use the first() method from the String class to achieve our goal. This method returns the first character of the string. This will return just the first character, even on a string with more than one character:
@Test
fun `converts string to char using first method`(){
val str = "H"
val char = str.first()
assertEquals('H', char)
}
We should use this method on a string when we are certain we are only interested in the first character of that string.
6. Using the toCharArray() Method
Lastly, we can leverage the toCharArray() method. This method returns an Array of characters representing the same sequence of characters as the string itself. Subsequently, we use the indexing operator on the array to retrieve the character at that index position.
Therefore, to convert a string of one character to a Char, we use the toCharArray() method with the zero index position:
@Test
fun `converts string to char using toCharArray method`(){
val str = "H"
val charArray = str.toCharArray()
val char = charArray[0]
assertEquals('H', char)
}
7. Conclusion
In this article, we explored various ways to convert a String to a Char in Kotlin. We used the get(), toCharArray(), and first() methods, all from the String class. Since these approaches rely on Kotlin built-in methods, they can easily be adopted in our Kotlin project.