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字符串的拉鏈操作