Convert an Int array to a String array in Kotlin
This article explores different ways to convert an Int array to a string array in Kotlin.
1. Using map() function
A simple and fairly efficient solution is to use the map() function to convert each value in the integer array to a String.
|
1 2 3 4 5 6 7 |
fun main() { val intArray = intArrayOf(1, 2, 3, 4, 5) val stringArray = intArray.map { it.toString() }.toTypedArray() println(stringArray.contentToString()) // [1, 2, 3, 4, 5] } |
2. Using for loop
Another approach is to use for-loops. The idea is to create a string array and assign values to it after converting the integer values to String.
|
1 2 3 4 5 6 7 8 9 10 |
fun main() { val intArray: IntArray = intArrayOf(1, 2, 3, 4, 5) val stringArray: Array<String?> = arrayOfNulls(intArray.size) for (i in intArray.indices) { stringArray[i] = intArray[i].toString() } println(stringArray.contentToString()) // [1, 2, 3, 4, 5] } |
That’s all about converting int array to string array in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)