Scala中的String chomp(或chop)函数
ChoporChompstring
它用于截断行尾字符。为此,Scala具有内置方法stripLineEnd。
语法:
string.stripLineEnd
在Scala中将字符串切成小段的程序
object MyClass {
def main(args: Array[String]) {
val str = "rfgdg\n"
println("The string is '" + str.stripLineEnd + "' ")
}
}输出:
The string is 'rfgdg'
可以这样创建一个字符串chomp方法,
def chomp(str: String) = str.stripLineEnd
示例
object MyClass {
// 定义
def chomp(str: String) = str.stripLineEnd
def main(args: Array[String]) {
val text = "Hello world!\n"
// 函数调用
println("chomp(text): " + chomp(text))
}
}输出:
chomp(text): Hello world!