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

编程知识 更新时间:2023-05-02 21:17:02

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程序将数组转换为字符串

本文发布于:2023-04-28 17:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/082c96b6bf0c39ec6ff054157bae3ad3.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字符串   转换为   程序   scala

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 109295文章数
  • 27730阅读数
  • 0评论数