Given a character array, we have to convert it into a string.
给定一个字符数组,我们必须将其转换为字符串。
Example:
例:
Input:
char array: ['i', 'n', 'd', 'i', 'a']
Output:
string = "india"
在Kotlin中将字符数组转换为字符串的程序 (Program to convert character array to string in Kotlin)
package com.includehelp.basic
import java.util.*
//Main Function entry Point of Program
fun main(args: Array<String>) {
//Input Stream
val scanner = Scanner(System.`in`)
//Input Char Array Size
print("Enter Char Array size: ")
val size = scanner.nextInt()
//Create Character array of Given size
val charArray = CharArray(size)
//Input array elements
println("Enter Char Arrays Elements:")
for (i in charArray.indices) {
print("charArray[$i] : ")
charArray[i] = scanner.next()[0]
}
//Print All Array Elements
println("Array : ${charArray.contentToString()} ")
//Get String from Cahr Array
var strFromArray = String(charArray)
//print String from Array
println("String Converted from Array : $strFromArray")
}
Output
输出量
Run 1:
-----
Enter Char Array size: 11
Enter Char Arrays Elements:
charArray[0] : i
charArray[1] : n
charArray[2] : c
charArray[3] : l
charArray[4] : u
charArray[5] : d
charArray[6] : e
charArray[7] : h
charArray[8] : e
charArray[9] : l
charArray[10] : p
Array : [i, n, c, l, u, d, e, h, e, l,p]
String Converted from Array : includehelp
-----
Run 2:
----
Enter Char Array size: 5
Enter Char Arrays Elements:
charArray[0] : i
charArray[1] : n
charArray[2] : d
charArray[3] : i
charArray[4] : a
Array : [i, n, d, i, a]
String Converted from Array : india
翻译自: https://www.includehelp.com/kotlin/convert-character-array-to-string.aspx