scala字符串的拉链操作
Scala字符串操作 (Scala strings operation)
A string is a very important datatype in Scala. This is why there are a lot of operations that can be done on the string object. Since the regular operations like addition, subtraction is not valid for a string, therefore, special operations like concatenation, comparison are defined.
字符串是Scala中非常重要的数据类型。 这就是为什么可以对字符串对象执行许多操作的原因。 由于常规操作(如加法,减法)对于字符串无效,因此,定义了特殊操作(如串联,比较)。
In this tutorial, we will share some of the important and common string functions.
在本教程中,我们将分享一些重要和通用的字符串函数 。
1)字符串相等(==) (1) String Equality (==))
The tradition equality operator == is also available in the string. You can use it to find equality of two string and the equality results in a boolean value.
字符串中也可以使用传统的等于运算符== 。 您可以使用它来查找两个字符串的相等性,并且相等性会产生布尔值。
str1 = str2 // this will return either TRUE OR FALSE
For Example,
例如,
val str1 = "Include"
val str2 = "Includes"
str1 == str2 // this will be false
2)字符串长度 (2) String Length)
There is an inbuilt function that is used to find the number of characters in a string. It includes all the space that comes it between. It is used to set the limit for string traversal.
有一个内置函数,用于查找字符串中的字符数。 它包括介于两者之间的所有空间。 用于设置字符串遍历的限制。
// this outputs a positive integer denoting the
// number of characters in the array.
str1.length
For example,
例如,
val str1 = "Include help"
str1.length // this will print 12
3)串连音 (3) String concat)
You can concatenate a string on other. It means the string will be appended on the calling string.
您可以在其他字符串上串联一个字符串。 这意味着该字符串将附加在调用字符串上。
str1.concat(str2)
for example,
例如,
val str1 = "Include"
val str2 = "Help"
str1.concat(str2) // This will print IncludeHelp
4)字符串charAt()方法 (4) String charAt() method )
To print the character at a specific index of the string the charAt method is used. The input is a zero-based index and output will be the corresponding character. Will throw an error if the input is greater than the length of the string.
要在字符串的特定索引处打印字符,请使用charAt方法。 输入是从零开始的索引,输出将是相应的字符。 如果输入大于字符串的长度,将引发错误。
str1.charAt(n)
For example,
例如,
val name = "Include"
name.charAt(4) // This will output u.
5)indexOf()方法 (5) indexOf() method)
The indexOf() method is used to check the index of a character in a string. This method returns an integer which is positive within the length of the string when the character is found in the array otherwise -1 is given as output.
indexOf()方法用于检查字符串中字符的索引。 当在数组中找到字符时,此方法返回一个整数,该整数在字符串的长度内为正,否则将给出-1作为输出。
str1.indexOf("a")
For example,
例如,
val name = "Include"
name.indexOf("d") // This will output 5.
6)Substring()方法 (6) Substring() method)
The substring method is used to define a substring from the calling string. It makes a new string with the specified part of the string.
substring方法用于从调用字符串中定义一个子字符串。 它使用字符串的指定部分创建一个新字符串。
str2 = str1.substring(startIndex , endIndex)
For example,
例如,
val name = "IncludeHelp is awesome"
name.substring(14 , 21) // This will output awesome.
Example code that uses all these functions
使用所有这些功能的示例代码
object MyClass {
def main(args: Array[String]) {
val str1 = "include Help "
val str2 = "is awesome"
println("str1 = " + str1)
println("str2 = " + str2)
println("Comparison between str1 and str2 is " + (str1 == str2))
println("The length of str1 is " + str1.length)
println("Concatenating str1 with str2 gives \n " + str1.concat(str2))
println("The character at index 5 of str2 is" + str2.charAt(5))
println("The index of 'c' in str1 is " + str1.indexOf("c"))
}
}
Output
输出量
str1 = include Help
str2 = is awesome
Comparison between str1 and str2 is false
The length of str1 is 13
Concatenating str1 with str2 gives
include Help is awesome
The character at index 5 of str2 ise
The index of 'c' in str1 is 2
翻译自: https://www.includehelp.com/scala/operation-on-strings-in-scala.aspx
scala字符串的拉链操作