Scala多行字符串

在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
  }


 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值