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 字符串占位符