scala 字符串转换数组_Scala程序将数组转换为字符串

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.com/scala/program-to-convert-array-to-string.aspx

scala 字符串转换数组

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值