博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
scala 字符串转换数组_Scala程序将数组转换为字符串
阅读量:2530 次
发布时间:2019-05-11

本文共 1958 字,大约阅读时间需要 6 分钟。

scala 字符串转换数组

Scala | 将数组转换为字符串 (Scala | Converting array to string)

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

翻译自:

scala 字符串转换数组

转载地址:http://qwozd.baihongyu.com/

你可能感兴趣的文章
Android dex分包方案
查看>>
ThreadLocal为什么要用WeakReference
查看>>
删除本地文件
查看>>
FOC实现概述
查看>>
base64编码的图片字节流存入html页面中的显示
查看>>
这个大学时代的博客不在维护了,请移步到我的新博客
查看>>
GUI学习之二十一——QSlider、QScroll、QDial学习总结
查看>>
nginx反向代理docker registry报”blob upload unknown"解决办法
查看>>
gethostbyname与sockaddr_in的完美组合
查看>>
kibana的query string syntax 笔记
查看>>
旋转变换(一)旋转矩阵
查看>>
thinkphp3.2.3 bug集锦
查看>>
[BZOJ 4010] 菜肴制作
查看>>
C# 创建 读取 更新 XML文件
查看>>
KD树
查看>>
VsVim - Shortcut Key (快捷键)
查看>>
C++练习 | 模板与泛式编程练习(1)
查看>>
HDU5447 Good Numbers
查看>>
08.CXF发布WebService(Java项目)
查看>>
java-集合框架
查看>>