scala中字符串计数
Trimming a string is the method of removing extra spaces from the string. It can be left removal, right removal, or removal of all spaces from the string.
修剪字符串是从字符串中删除多余空格的方法。 可以左移,右移或从字符串中移走所有空格。
To remove blank spaces from a string, Scala provides a trim() method. The trim() will remove spaces from both sides, i.e. before the characters(leading) and from the end(trailing).
为了从字符串中删除空格,Scala提供了trim()方法。 trim()将删除两侧的空格,即在字符(前导)之前和结尾(尾部)之间。
Syntax:
句法:
string.trim()
The method doesn't accept any parameter and returns a string with spaces removed from it.
该方法不接受任何参数,并返回从中删除空格的字符串。
程序使用trim()删除空格字符串 (Program to remove spaces string using trim())
object myObject {
def main(args: Array[String]) {
val string = " Hello! Learn scala programming at IncludeHelp "
println("Orignal string is '" + string + "' with leading and trailing spaces")
val trimmedString = string.trim
println("The trimmed string is '" + trimmedString + "'")
}
}
Output
输出量
Orignal string is ' Hello! Learn scala programming at IncludeHelp ' with leading and trailing spaces
The trimmed string is 'Hello! Learn scala programming at IncludeHelp'
从左或右修剪字符串 (Trimming string from left or right)
We can optionally trim a string is scala from the left (removing leading spaces) called left-trim and from the right (removing trailing spaces) called right-trim.
我们可以有选择地从左 (删除前导空格) 到右修剪(从尾部删除空格)修剪从 scala开始的字符串 。
This is done using the replaceAll() methods with regex. We will find all the space (right/left) using the regular expression and then replace them with "".
这可以通过使用regex的replaceAll()方法来完成。 我们将使用正则表达式查找所有空间(右/左),然后将其替换为“” 。
删除左,右空格的程序 (Program to remove left and right spaces)
object MyClass {
def main(args: Array[String]) {
val string = " Hello! Learn scala programming at IncludeHelp "
println("The string is '" + string + "'")
val leftTrimString = string.replaceAll("^\\s+", "")
println("Left trimmed string is '" + leftTrimString + "'")
val rightTrimString = string.replaceAll("\\s+$", "")
println("Right trimmed string is '" + rightTrimString + "'")
}
}
Output
输出量
The string is ' Hello! Learn scala programming at IncludeHelp '
Left trimmed string is 'Hello! Learn scala programming at IncludeHelp '
Right trimmed string is ' Hello! Learn scala programming at IncludeHelp'
翻译自: https://www.includehelp.com/scala/how-to-left-trim-and-right-trim-strings-in-scala.aspx
scala中字符串计数