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中將字節數組轉換為字符串,我們有兩種方法,

  1. Using new keyword

    使用新關鍵字

  2. 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 String
object 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 String
object 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).mkString
println("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方法打印該字符串。

翻譯自: https://www.includehelp.com/scala/how-to-convert-byte-array-to-string-in-scala.aspx

scala 字符串轉換數組

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541693.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541693.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541693.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

智能關機軟件 c語言,智能關機軟件

智能關機軟件是一款免費共享關機軟件。智能關機軟件不但具有定時關機、自動關機的功能,而且還可以進行定時提醒信息、打開文件、打開網頁、重啟計算機、注銷用戶、鎖定計算機、計算機休眠、計算機待機、關閉顯示器,并且可以進行多任務計劃,可…

wget: command not found

-bash: wget: command not found的兩種解決方法 今天給服務器安裝新LNMP環境時,wget 時提示 -bash:wget command not found,很明顯沒有安裝wget軟件包。一般linux最小化安裝時,wget不會默認被安裝。可以通過以下兩種方法來安裝:1、rpm 安裝rp…

數據庫數據規范化看不懂_數據庫管理系統中的規范化

數據庫數據規范化看不懂DBMS中的規范化 (Normalization in DBMS) Every table must have a single idea. The method by which we divide tables approximately is called normalization and the rest used for normalization is a functional dependency. For the normalizati…

c 語言開發一個四則運算器,C++實現四則運算器(無括號)

本文實例為大家分享了C實現無括號的四則運算器的具體代碼,供大家參考,具體內容如下完成度更高的帶括號版本可以看C實現四則運算器(帶括號)對于無括號的計算器,實現起來比較容易,下面讓我們一步步實現。舉例首先明確需要實現怎樣的…

iOS開發之解決系統數字鍵盤無文字時delete鍵無法監聽的技巧

最近在做用戶登錄獲取驗證碼時添加圖形驗證碼功能,就是只有正確輸入圖形驗證碼才能收到后臺發送的短信驗證碼。效果如下: 看起來雖然是個小功能,但是實際操作起來,會發現蘋果給我們留下的坑,當然更多的是自己給自己挖的…

c ++查找字符串_C ++結構| 查找輸出程序| 套裝1

c 查找字符串Program 1: 程序1&#xff1a; #include <iostream>#include <math.h>using namespace std;struct st {int A NULL;int B abs(EOF EOF);} S;int main(){cout << S.A << " " << S.B;return 0;}Output: 輸出&#xff1a…

二級c語言加油,二級C語言 備考指南及常見問題(2013版)

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓3、關于上機操作部分的復習最好買一本上機題庫方面的教材&#xff0c;或打印、閱讀南開百題之類的電子文檔。配合上機模擬軟件(無紙化考試軟件)&#xff0c;上機練習是必須的。上機軟件一般有100套題多一點&#xff0c;每套有程序填…

開放定址散列表

再散列之后散列函數要重新計算。 // kaifangliaobiao.cpp : 定義控制臺應用程序的入口點。 //使用平方探測解決沖突問題時&#xff0c;散列表至少空一半時&#xff0c;總能插入一個新的元素#include "stdafx.h" #include<iostream> using namespace std;#ifnde…

合并兩個鏈表數據結構c語言,合并兩個鏈表.

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓#include #define N1 10#define N2 10struct list{int date ;struct list *next;};main(){struct list *p1,*p2,*p3,*p4,*head,*head1,*head2,*p;int n0;head1head2NULL;p1p2(struct list *)malloc(sizeof(struct list));p1->da…

c ++查找字符串_C ++結構| 查找輸出程序| 套裝2

c 查找字符串Program 1: 程序1&#xff1a; #include <iostream>using namespace std;int main(){typedef struct{int A;char* STR;} S;S ob { 10, "india" };S* ptr;ptr &ob;cout << ptr->A << " " << ptr->STR[2];…

連接fiddler后手機無法顯示無網絡

升級了fiddler到4.6版本&#xff0c;手機設置代理后提示無網絡&#xff0c;試試以下解決方法&#xff1a; 1.fiddler升級后對應的.net framework也要升級&#xff0c;安裝最新的.net framework 4.6&#xff0c;升級安裝后&#xff0c;可以正確抓包啦 2.如果上述方法無效&#x…

android 人臉解鎖 鎖屏動畫,人臉保護鎖(人臉識別鎖屏)

這是一款十分炫酷的鎖屏工具&#xff0c;還記得電影中的特工所用的人臉識別鎖嗎&#xff1f;這款應用也能讓你過過癮&#xff01;人臉識別鎖屏安卓版是一款用人臉做密碼來打開手機屏保鎖的一個APP。不僅可以作屏保鎖&#xff0c;也可以單獨保護某些重要程序不被偷窺,例如查看短…

dbms_排名前50位的DBMS面試問答

dbms1) What are the drawbacks of the file system which is overcome on the database management system? 1)在數據庫管理系統上克服的文件系統有哪些缺點&#xff1f; Ans: Data redundancy & isolation, difficulty in accessing data, data isolation, and integri…

linux時間

CST代表中國標準時間rtc實時時鐘linux主要有兩種時間硬件時鐘 clock系統時鐘 date修改時間 date 03300924必須是兩位或者 date -s 2017:03:30將系統時間同步到硬件時間 hwclock -w將硬件時間同步到系統時間 hwclock -s轉載于:https://blog.51cto.com/12372297/1911608

查找Python中給定字符串的所有排列

Python itertools Module Python itertools模塊 "itertools" are an inbuilt module in Python which is a collection of tools for handling iterators. It is the most useful module of Python. Here, a string is provided by the user and we have to print a…

android 圖片疊加xml,Android實現圖片疊加效果的兩種方法

本文實例講述了Android實現圖片疊加效果的兩種方法。&#xff0c;具體如下&#xff1a;效果圖&#xff1a;第一種&#xff1a;第二種&#xff1a;第一種是通過canvas畫出來的效果:public void first(View v) {// 防止出現Immutable bitmap passed to Canvas constructor錯誤Bit…

Win10系列:VC++ 定時器

計時器機制俗稱"心跳"&#xff0c;表示以特定的頻率持續觸發特定事件和執行特定程序的機制。在開發Windows應用商店應用的過程中&#xff0c;可以使用定義在Windows::UI::Xaml命名空間中的DispatcherTimer類來創建計時器。DispatcherTimer類包含了如下的成員&#xf…

dbms系統 rdbms_DBMS與傳統文件系統之間的區別

dbms系統 rdbmsIntroduction 介紹 DBMS and Traditional file system have some advantages, disadvantages, applications, functions, features, components and uses. So, in this article, we will discuss these differences, advantages, disadvantages and many other …

android 百度地圖api密鑰,Android百度地圖開發獲取秘鑰之SHA1

最近在做一個關于百度地圖的開發。不過在正式開發之前還必須要在百度地圖API官網里先申請秘鑰&#xff0c;而在申請秘鑰的過程中&#xff0c;就需要獲取一個所謂的SHA1值。如上所示&#xff0c;但是由于不是正式開發&#xff0c;所以以上的發布版和開發版的SHA1可以先填寫相同。…

單位矩陣的逆| 使用Python的線性代數

Prerequisites: 先決條件&#xff1a; Defining a Matrix 定義矩陣 Identity Matrix 身份矩陣 There are matrices whose inverse is the same as the matrices and one of those matrices is the identity matrix. 有些矩陣的逆與矩陣相同&#xff0c;并且這些矩陣之一是單位…