Home »
Scala
Strings in Scala
By IncludeHelp Last updated : October 20, 2024
Scala strings
A string is a sequence of characters. In Scala, the String object is immutable. This means that the strings in Scala cannot be modified in the program. Its value given at the time of creation will remain constant throughout the program.
You can think of the string as a sequence of characters i.e. an array of characters that are "0" indexed. This means the first character has index 0, the second character has index 1, and so on. Every single character from the string can be extracted by using its index number.
Example
For example, suppose we have an array "arr" which defines some text, we can access any character by using its index number.
def arr = "Include Help is awesome"
arr(4) // Output will be u
arr(11) // Output will be p
arr(34) // Output will give error array out of bond
// and a list of error related to that
Importing String Class
All operations on the Scala string are defined in the String class. Which can be imported from "java.lang.String".
Defining string
There are two syntaxes that can be used to define a string in Scala:
1) The object is created and a string is fed to it,
var name = "Include Help"
//or
val name = "Include Help"
2) The object of String type is created using specifier and then the string is fed to it,
var name : Sting = "Include Help"
//pr
val name : Sting = "Include Help"
Example of Scala String
object MyClass {
// A program to show implementation of scala string
def main(args: Array[String]) {
var name = "Include Help"
println("Welcome" + name)
println("the 5th character of you name is" + name(5));
}
}
Output
WelcomeInclude Help
the 5th character of you name isd
Explanation
In the above example, we have created a string named name and Fed to it the value "include help". Then we have printed the string using the println() function. Also, we have printed the fifth character of the string using the index selection method of printing character values of a string that we have discussed previously in this tutorial.
There are many functions defined over the string object in the string class and We will be going through them in the next tutorial.
Formatting strings
The format() method in Scala is from the stringlike trait which holds methods for string operations. This method is used to format strings.
Example
object MyClass {
def main(args: Array[String]) {
var output = "%s earns %d in a month!"
var employee_name = "Manju"
var employee_salary = 400000
var formatted_string = output.format(employee_name, employee_salary)
print(formatted_string)
}
}
Output
Manju earns 400000 in a month!
String concatenation
String concatenation is joining two different strings to form a single one. Scala provides two methods to concatenate strings. They are:
- concat() method
- '+' operator
Example
object myObject {
def main(args: Array[String]) {
val string1 = "Scala "
val string2 = "Programming Language"
val concatString1 = string1.concat(string2)
val concatString2 = string1 + string2
println("The concatenated string (1) is " + concatString1)
println("The concatenated string (2) is " + concatString2)
}
}
Output
The concatenated string (1) is Scala Programming Language
The concatenated string (2) is Scala Programming Language
String interpolation
String interpolation is defining a string literal with one or more placeholders, these placeholders are to be replaced with variables or expressions while the final compilation of the result.
Example
object MyClass {
def main(args: Array[String]) {
var bikename = "KTM Duke 390"
var topspeed = 185
var str = s"I have $bikename and it has a top speed of $topspeed "
println(str)
}
}
Output
I have KTM Duke 390 and it has a top speed of 185