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,

使用的方法

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值