scala 字符串占位符_如何在Scala中将带有换行符的字符串转换为字符串列表?
scala 字符串占位符
A string is a sequence of characters and it can contain multiple lines, for this, the string uses the newline character \n. And, we can separate each newline into a string and store them as a list of strings.
字符串是字符序列,可以包含多行,為此,字符串使用換行符\ n 。 并且,我們可以將每個換行符分隔成一個字符串,并將它們存儲為字符串列表。
For this purpose, we can use some methods that are built-in in the Scala language. The logics rest the same, storing all contents in a string until a newline is encountered and after the newline, the contents till the next are stored in the second string of the sequence and so on.
為此,我們可以使用Scala語言內置的一些方法。 邏輯保持不變,將所有內容存儲在字符串中,直到遇到換行為止;在換行之后,直到下一行的內容都存儲在序列的第二個字符串中,依此類推。
Methods that are used,
使用的方法
string.split('char'):
string.split('char') :
This function splits the string after the occurrence of the specified character. This means that when the character occurs the string will get split.
此函數在出現指定字符后分割字符串。 這意味著當字符出現時,字符串將被分割。
toVector:
toVector :
This method stores this split string into a list that is to be returned.
此方法將此拆分字符串存儲到要返回的列表中。
Program:
程序:
object MyClass {def convertStringToSeq(s: String): Seq[String] =s.split("\n").toVector def main(args: Array[String]) {val str = "Hello!\nthisis\nInclude Help"val conlist = convertStringToSeq(str)println(conlist)}}Output
輸出量
Vector(Hello!, thisis, Include Help)翻譯自: https://www.includehelp.com/scala/how-to-convert-a-string-with-newline-into-a-list-of-strings-in-scala.aspx
scala 字符串占位符
總結
以上是生活随笔為你收集整理的scala 字符串占位符_如何在Scala中将带有换行符的字符串转换为字符串列表?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 对称密码和非对称密码体系_密码学类型:对
- 下一篇: 在Ruby中使用&运算符(new_arr