200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > scala 字符串转换数组_如何在Scala中将字节数组转换为字符串?

scala 字符串转换数组_如何在Scala中将字节数组转换为字符串?

时间:2023-01-31 16:17:00

相关推荐

scala 字符串转换数组_如何在Scala中将字节数组转换为字符串?

scala 字符串转换数组

Byte Array in Scala is an array of elements of a byte type. String in Scala is a collection of the character data type.

Scala中的字节数组是字节类型的元素的数组。 Scala中的String是字符数据类型的集合。

将字节数组转换为字符串 (Convert byte array to string)

For converting a byte array to string in Scala, we have two methods,

为了在Scala中将字节数组转换为字符串,我们有两种方法,

Using new keyword

使用新关键字

Using mkString method

使用mkString方法

1)使用new关键字将字节数组转换为字符串 (1) Converting byte array to string using new keyword)

In this method, we will convert the byte array to a string by creating a string using the new String() and passing the byte array to the method.

在此方法中,我们将通过使用新的String()创建字符串并将字节数组传递给该方法,将字节数组转换为字符串。

Syntax:

句法:

val string = new String(byte_array)

Program to convert a byte array to string

程序将字节数组转换为字符串

// Program to convert Byte Array to Stringobject MyObject {def main(args: Array[String]) {val byteArray = Array[Byte](73, 110, 99, 108, 117, 100, 101, 104, 101, 108, 112)val convertedString = new String(byteArray) println("The converted string '" + convertedString + "'")}}

Output:

输出:

The converted string 'Includehelp'

Explanation:

说明:

In the above code, we have created a byte array named byteArray and converted it to a string by passing the byteArray while creating a string named convertedString using new String().

在上面的代码中,我们创建了一个名为byteArray的字节数组,并在使用new String()创建一个名为convertedString的字符串的同时,通过传递byteArray将其转换为字符串。

2)使用mkString方法将字节数组转换为String (2) Converting byte array to String using mkString method)

We can use the mkString method present in Scala to create a string from an array. But prior to conversion, we have to convert byte array to character array.

我们可以使用Scala中存在的mkString方法从数组创建字符串。 但是在转换之前,我们必须将字节数组转换为字符数组。

Syntax:

句法:

(byteArray.map(_.toChar)).mkString

Program to convert byte array to String using mkString method

程序使用mkString方法将字节数组转换为String

// Program to convert Byte Array to Stringobject MyObject {def main(args: Array[String]) {val byteArray = Array[Byte](73, 110, 99, 108, 117, 100, 101, 104, 101, 108, 112)val convertedString = byteArray.map(_.toChar).mkStringprintln("The converted string '" + convertedString + "'")}}

Output:

输出:

The converted string 'Includehelp'

Explanation:

说明:

In the above code, we have used the mkString method to convert a byte array to string. We have created a byte Array named byteArray and then used the mkString method to convert it to a string. But before conversion, we have converted the byte to their character equivalent using .map(_.toChar) method. The result of this is stored to a string named convertedString which is then printed using println method.

在上面的代码中,我们使用了mkString方法将字节数组转换为字符串。 我们创建了一个名为byteArray的字节数组,然后使用mkString方法将其转换为字符串。 但是在转换之前,我们已经使用.map(_。toChar)方法将字节转换为等效的字符。 其结果存储到一个名为convertedString的字符串中,然后使用println方法打印该字符串。

翻译自: /scala/how-to-convert-byte-array-to-string-in-scala.aspx

scala 字符串转换数组

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。