添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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,

使用的方法

  1. 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.

    此函数在出现指定字符后分割字符串。 这意味着当字符出现时,字符串将被分割。

  2. 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编程系列教程的第二部分,具体参考了我2011年秋季的课程《计算语言学入门》。 您可以在此博客上查看其他教程。 它们也列在课程的链接页面上。 本教程重点介绍元组和列表,这是用于处理元素组的两种构造。 没有后者,您将无法完成很多工作,而前者是如此有用,您可能会发现自己经常使用它们。 在上一教程中,我们看到了如何将单个... def main(args: Array[String]) { // 若一行里仅有一个语句,';'可不写。另一方面,如果一行里写多个语句那么分号是需要的 val s = "教程"; println(s) linux/unix下的文本文件换行符:\r Mac下的文本文件换行符:\n 为什么要用System.getProperty(“line.separator”)代替固定格式的换行符: 具备平台无关性 一次编写,到处运行 并不是说具备了平台无关性,保险这些好处就可以总是这