1. Introduction
Converting a string to a string array is a common programming task that can help us by transforming data to a format that is better suited for certain operations.
In this tutorial, we’ll explore different methods to convert a string to a string array in Kotlin. We’ll focus on the example of converting a String of words into an Array
2. Using the split() Method
We can use Kotlin’s own split() method to convert a string to an array of strings based on a specified delimiter. We’ll use the ” ” delimiter to split a string into words:
@Test
fun `convert using split() method`(){
val sentence = "this is a sentence"
val expectedArr = arrayOf("this", "is", "a", "sentence")
val actualArr = sentence.split(" ").toTypedArray()
assertArrayEquals(expectedArr, actualArr)
}
Alternatively, we can make use of regular expressions by using the split() method of the Regex class to convert a string to an array of substrings on the basis of a regular expression pattern. Simply put, we supply the split() method with a sentence and call it on a Regex pattern that matches one or more whitespace characters.
What’s more, this method behaves well for multiline strings too:
@Test
fun `convert using Regex`() {
val sentence = "this is an example\n" +
"of a multiline sentence\n" +
"in Kotlin programming"
val expectedArr = arrayOf("this", "is", "an", "example", "of", "a", "multiline", "sentence", "in",
"Kotlin", "programming")
val pattern = "\\s+".toRegex()
val actualArr = pattern.split(sentence).toTypedArray()
assertArrayEquals(expectedArr, actualArr)
}
3. Using the StringTokenizer Class
The StringTokenizer class provides us a way to tokenize our string into substrings with respect to a delimiter.
This method is great for multiline strings as well:
@Test
fun `convert using StringTokenizer`() {
val sentence = "this is an example\n" +
"of a multiline sentence\n" +
"in Kotlin programming"
val expectedArr = arrayOf("this", "is", "an", "example", "of", "a", "multiline", "sentence", "in",
"Kotlin", "programming")
val st = StringTokenizer(sentence)
val actualArr = Array(st.countTokens()) { st.nextToken() }
assertArrayEquals(expectedArr, actualArr)
}
So, we create a new array with the same size as the StringTokenizer object and then iterate the substrings in this object. Lastly, we add each substring to the new array.
4. Using the StringReader and BufferedReader Classes
Finally, Kotlin’s StringReader and BufferedReader classes can help iterate a string character-wise and add each character to an array until we reach a delimiter.
Unlike the split() method above, this method works perfectly for multiline strings as well:
@Test
fun `convert using StringReader and BufferedReader`() {
val sentence = "this is an example\n" +
"of a multiline sentence\n" +
"in Kotlin programming"
val expectedArr = arrayOf("this", "is", "an", "example", "of", "a", "multiline", "sentence", "in",
"Kotlin", "programming")
val reader = BufferedReader(StringReader(sentence))
val actualArr = reader.readLines().flatMap { it.split(" ") }.toTypedArray()
assertArrayEquals(expectedArr, actualArr)
}
Using the BufferedReader instance, we read the lines from the sentence using the readLines() method. For each line, we use the split() method to split into words and subsequently, we collect the words into an array using flatMap().
5. Conclusion
In this article, we’ve explored different methods to convert a string to an array of strings in Kotlin. In brief, we’ve looked at how the split() and Regex approaches can help us achieve this. Furthermore, we learned how to achieve the same goal by using classes such as StringTokenizer and BufferedReader.
As always, the code samples can be found over on GitHub.