Home »
Scala
Scala String Operations
By IncludeHelp Last updated : October 20, 2024
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.
In this tutorial, we will share some of the important and common string functions.
1. String Equality (==)
The traditional equality operator == is used to compare two strings. It checks if the values of the two strings are equal and returns a boolean result (true or false).
Syntax
str1 == str2 // returns TRUE or FALSE
Example
val str1 = "Include"
val str2 = "Includes"
val result = str1 == str2 // This will return false
println(result) // Output: false
2. String Length
The length method is used to find the number of characters in a string, including spaces. This method is helpful when you need to traverse or limit operations on a string based on its length.
Syntax
// this outputs a positive integer denoting the
// number of characters in the array.
str1.length
Example
val str1 = "Include help"
println(str1.length) // this will print 12
3. String Concatenate
You can concatenate a string with another string. It means the second string will be appended to the first string.
Syntax
str1.concat(str2)
Example
val str1 = "Include"
val str2 = "Help"
val result = str1.concat(str2)
println(result) // This will print "IncludeHelp"
4. Getting Characters from a String
The charAt() method is used to return the character at a specific index in a string. The index is zero-based, meaning it starts at 0. If the input index is greater than the length of the string, the method will throw an error.
Syntax
str1.charAt(n)
Example
val name = "Include"
name.charAt(4) // This will output 'u'.
5. Finding Characters from a String
The indexOf() method returns the index of the first occurrence of a specified character in a string. It returns an integer, which will be a non-negative value within the length of the string if the character is found, otherwise, it returns -1.
Syntax
str1.indexOf("a")
Example
val name = "Include"
name.indexOf('d') // This will output 5.
6. Extracting Substring from a String
The substring() method is used to extract a part of the string between two specified indices (start index, inclusive; end index, exclusive). The new substring is a portion of the original string.
Syntax
str2 = str1.substring(startIndex , endIndex)
Example
val name = "IncludeHelp is awesome"
name.substring(14, 21) // This will output "awesome".
Example of Scala String Operations
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