Home »
Scala
String Concatenation in Scala
By IncludeHelp Last updated : October 20, 2024
String is an immutable collection that stores sequences of characters. There are various operations and methods defined on strings in Scala for proper functioning of the collections. One of them is concatenation.
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
The concat() method for string concatenation
The concat() method is used to add two strings and return a new method containing the character form both strings.
Syntax
string1.concat(string2)
Parameters
The method accepts a single parameter which is the string to be added.
Return type
It returns the concatenated string.
Program to illustrate the working of concat() method
object myObject {
def main(args: Array[String]) {
val string1 = "Scala "
val string2 = "Programming Language"
val concatString = string1.concat(string2)
println("The concatenated string is " + concatString)
}
}
Output:
The concatenated string is Scala Programming Language
The '+' operator for string concatenation
The '+' operator adds the two strings and returns the result of addition.
Syntax
string1 + string2
Program to illustrate the working of '+' operator
object myObject {
def main(args: Array[String]) {
val string1 = "Scala "
val string2 = "Programming Language"
val concatString = string1 + string2
println("The concatenated string is " + concatString)
}
}
Output:
The concatenated string is Scala Programming Language