美文网首页Scala专辑
Scala多行字符串

Scala多行字符串

作者: SunnyMore | 来源:发表于2018-02-09 15:55 被阅读44次

在Scala中可以用"""的方式创建多行字符串,eg.

object StringTest {
  def main(args: Array[String]): Unit = {
    val s1 ="""This is
        my first time
        to learn Scala"""
    println(s1)
  }
}

输出如下:

This is
        my first time
        to learn Scala

但是这种方式有个问题,就是每行的开头可能会有空格;如何消除空格,可以在多行字符串的结尾添加stripMargin方法,并且在第二行开始的每一行前面添加|,eg.

object StringTest {
  def main(args: Array[String]): Unit = {
    val s1 ="""This is
        |my first time
        |to learn Scala""".stripMargin
    println(s1)
  }
}

输出如下:

This is
my first time
to learn Scala

如果不想使用|,也可以使用其他任意字符,eg.

object StringTest {
  def main(args: Array[String]): Unit = {
    val s2 ="""This is
              #my first time
              #to learn Scala""".stripMargin('#')
    println(s2)
  }
}

输出如下:

This is
my first time
to learn Scala

此处需要注意,只能使用stripMargin('#'),而不能使用stripMargin("#"),这个会出现Cannot resolve reference stripMargin with such signature;
Scala的多行,其实在每个换行的字符串之间生成\n的换行符,可以将替换掉,eg.

val s3 ="""This is
              #my first time
              #to learn Scala""".stripMargin('#').replaceAll("\r\n", " ")
    println(s3)

输出如下:

This is my first time to learn Scala

这个replace需要注意的是,在编辑器中需要使用replaceAll("\r\n", " "),但是在scala的REPL中只需要使用replaceAll("\n", " ");原因如下:

Your Idea making the newline marker the standard Windows \r\n, so you've got "abcd\r\nefg". The regex is turning it into "abcd\refg" and Idea console is treaing the \r slightly differently from how the windows shell does. The REPL is just using \n as the new line marker so it works as expected.

Solution 1: change Idea to just use \n newlines.

Solution 2: don't use triple quoted strings when you need to control newlines, use single quotes and explicit \n characters.

Solution 3: use a more sophisticated regex to replace \r\n, \n, or \r

Scala多行字符串的另外一个功能是,单引号和双引号无需进行转义,eg.

val s4 ="""This is
              #'my' first time
              #to "learn" Scala""".stripMargin('#').replaceAll("\r\n", " ")
    println(s4)

输出如下:

This is 'my' first time to "learn" Scala

stripMargin函数在StringLike.scala源码中默认使用了|这个字符串;

/** For every line in this string:
   *
   *  Strip a leading prefix consisting of blanks or control characters
   *  followed by `|` from the line.
   */
  def stripMargin: String = stripMargin('|')

/** For every line in this string:
   *
   *  Strip a leading prefix consisting of blanks or control characters
   *  followed by `marginChar` from the line.
   */
  def stripMargin(marginChar: Char): String = {
    val buf = new StringBuilder
    for (line <- linesWithSeparators) {
      val len = line.length
      var index = 0
      while (index < len && line.charAt(index) <= ' ') index += 1
      buf append
        (if (index < len && line.charAt(index) == marginChar) line.substring(index + 1) else line)
    }
    buf.toString
  }

相关文章

  • scala之stripMargin's demo

    在Scala代码块中利用“定界符”创建多行字符串 解决方法: 在Scala中,利用三个双引号包围多行字符串配合...

  • Scala多行字符串

    在Scala中可以用"""的方式创建多行字符串,eg. 输出如下: 但是这种方式有个问题,就是每行的开头可能会有空...

  • Scala学习笔记

    1 Scala入门 scala Hello World Scala变量 Scala变量 Scala字符串 Scal...

  • Scala使用fastJson整理

    内容整理 字符串Map => JSONObject => Scala Map Scala Map => JSONO...

  • TypeScript学习

    字符串的新特性 多行字符串 使用``实现多行字符串 编译成js代码为: 字符串模板 其实和多行字符串差不多 编译j...

  • Javascript知识点整合

    字符串 单行字符串: ‘字符串’或“字符串” 多行字符串: `多行字符串` 字符串操作: 字符串连接‘+’号 长度...

  • ES6-字符串模板与字符串新增的方法

    ES6模板字面量(template literal) 多行字符串 ES5情况下多行字符串 多行的字符串以 逗号 '...

  • TypeScript 新特性介绍

    TypeScript 新特性介绍 字符串新特性 多行字符串 JavaScript 定义多行字符串 TypeScri...

  • 数据类型

    字符串 ''' ''' 多行字符串 r' ' 字符串不进行转义, 也可以跟多行字符串一起使用 r''' '''' ...

  • 字符串/数组操作+Map/Set

    一:字符串1.多行字符串console.log(多行 字符串 测试); 2.模版字符串var name = 'wa...

网友评论

    本文标题:Scala多行字符串

    本文链接:https://www.haomeiwen.com/subject/nrwftftx.html