Kotlin program to convert character array to string

Introduction :

Sometimes we need to convert one character array to a String. For example, if the array is [‘a’,‘b’,‘c’,‘d’,‘e’], we may need to convert it to string abcde or a-b-c-d-e etc. It is pretty easy in kotlin to convert one character array to string. In this tutorial, I will show you two different ways to do that.

Method 1: Using String constructor :

Kotlin String comes with a lot of useful constructors. We can pass one character array and it will convert it to a string. Following are the two variants of this constructor :

String(chars: CharArray): String

String(chars: CharArray, offset: Int, length: Int): String

The first one is straight forward. It takes one character array and returns one string. The second one takes two additional parameters. offset defines the number of characters we need to exclude from start and length is the final string length.

If the sum of offset and length is out of bound or if any one of these two values is less than 0, it will throw one IndexOutOfBoundsException.

fun main() {
    val charArr = charArrayOf('a','b','c','d','e')

    var str1 = String(charArr)
    val str2 = String(charArr,1,3)
    
    println("str1 : $str1")
    println("str2 : $str2")
}

It will print the below output :

str1 : abcde
str2 : bcd

Kotlin char array to string constructor

Here, str1 is a string with all the characters of the character array charArr. For str2, we are also passing offset as 1 and length as 3. So, it excludes the first character and the final string is of size 3.

Using contentToString() :

contentToString is a collection function that returns the string conversion for a character array :

fun main() {
    val charArr = charArrayOf('a','b','c','d','e')

    var str1 = charArr.contentToString()

    println("str1 : $str1")
}

It will print :

str1 : [a, b, c, d, e]

Kotlin char array to string contentToString

Using joinToString() :

joinToString is another function to convert a character array to string in Kotlin. It actually joins the characters to a string. Optionally, we can provide one separator to separate each character, prefix, postfix, and limit. It is defined as below :

fun CharArray.joinToString(
    separator: CharSequence = ", ",
    prefix: CharSequence = "",
    postfix: CharSequence = "",
    limit: Int = -1,
    truncated: CharSequence = "...",
    transform: ((Char) -> CharSequence)? = null
): String

If you don’t pass any of these parameters, it will simply return the string formed using the characters separated by a comma. For example :

fun main() {
    val charArr = charArrayOf('a','b','c','d','e')

    var str1 = charArr.joinToString()

    println("str1 : $str1")
}

It will print :

str1 : a, b, c, d, e

Comma is used because ”, ” is the default separator. You can pass other parameters to change the output format :

fun main() {
    val charArr = charArrayOf('a','b','c','d','e')

    var str1 = charArr.joinToString("-","$","&",3,"end")

    println("str1 : $str1")
}

Here, separator is - prefix is $ postfix is & limit is 3 truncated is end

It will print the below output :

str1 : $a-b-c-end&

Kotlin char array to string joinToString

Where is the color and why codevscolor ?

Long story short, I love paintings and I paint on weekends. We(me and my wife) have one Youtube channel. Below is a video that I did recently. If you love this please do subscribe to support us 😊