scala 字符串转换数组
Scala | 将数组转换为字符串 (Scala | Converting array to string)
Arrays play an important role in programming as they provide easy operation and there is a large amount of method available in the Scala library of array manipulation. But there are times when storing or printing of array can be done more effectively when converted to a string, but the question is how? So, here is a program to convert array to string in Scala.
数组在编程中起着重要作用,因为它们提供了简单的操作,并且Scala数组操作库中提供了大量可用的方法。 但是有时将数组转换为字符串后 ,可以更有效地完成数组的存储或打印,但是问题是如何呢? 因此,这是一个在Scala中将数组转换为字符串的程序 。
The mkString() method of the array library is employed to perform the task of conversion of array to string.
数组库的mkString()方法用于执行将数组转换为string的任务。
Syntax:
句法:
array_name.mkString(saperator)
The method takes a separator as a parameter which will separate two array elements in the string and returns a string.
该方法将分隔符作为参数,该分隔符将分隔字符串中的两个数组元素并返回一个字符串。
Program:
程序:
object myObject {
def main(args: Array[String]) {
val myArray = Array("Learn", "programming", "at", "IncludeHelp")
val str = myArray.mkString(" ")
print("myArray : "+str)
}
}
Output
输出量
myArray : Learn programming at IncludeHelp
You can use any string as a separator for your converted string, in the above program we have used a single space as a separator which is commonly used. Common separators that are used in converting an array to string are , , \n (new line), "".
您可以使用任何字符串作为转换后的字符串的分隔符,在上述程序中,我们使用单个空格作为常用分隔符。 了在一个阵列转换为字符串使用的常见分离器是,\ n(换行符)“”。
In the case of converting an array to string whose elements are numbers, we use , comma as separator else all the elements will appear like a single big number.
在数组转换为字符串,其元素是数字,我们使用,逗号作为分隔符的情况下,其他所有的元素会出现像一个大数目。
Program:
程序:
object myObject {
def main(args: Array[String]) {
val myArray = Array(12, 5, 45, 56)
val str = myArray.mkString(", ")
print("myArray : "+str)
}
}
Output
输出量
myArray : 12, 5, 45, 56
翻译自: https://www.includehelp/scala/program-to-convert-array-to-string.aspx
scala 字符串转换数组
更多推荐
scala 字符串转换数组_Scala程序将数组转换为字符串
发布评论