A string is a sequence of characters. In Scala, objects of String are immutable which means a constant and cannot be changed once created. when a new string is created by adding two strings is known as a concatenation of strings. Scala provides concat() method to concatenate two strings, this method returns a new string which is created using two strings. we can also use ‘+’ operator to concatenate two strings.
Syntax:
str1.concat(str2); Or "str1" + "str2";
Below is the example of concatenating two strings.
Using concat() method: This method appends argument to the string.
Example #1:
// Scala program to illustrate how to // concatenate strings object GFG { // str1 and str2 are two strings var str1 = "Welcome! GeeksforGeeks " var str2 = " to Portal" // Main function def main(args: Array[String]) { // concatenate str1 and str2 strings // using concat() function var Newstr = str1.concat(str2); // Display strings println("String 1:" +str1); println("String 2:" +str2); println("New String :" +Newstr); // Concatenate strings using '+' operator println("This is the tutorial" + " of Scala language" + " on GFG portal"); } } |
Output:
String 1:Welcome! GeeksforGeeks String 2: to Portal New String :Welcome! GeeksforGeeks to Portal This is the tutorial of Scala language on GFG portal
In above example, we are joining the second string to the end of the first string by using concat() function. string 1 is Welcome! GeeksforGeeks string 2 is to Portal. After concatenating two string we get new string Welcome! GeeksforGeeks to Portal.
Using + operator : we can add two string by using + operator.
Example #2:
// Scala program to illustrate how to // concatenate strings // Creating object object GFG { // Main method def main(args: Array[String]) { var str1 = "Welcome to "; var str2 = "GeeksforGeeks"; // Concatenating two string println("After concatnate two string: " + str1 + str2); } } |
Output:
After concatnate two string: Welcome toGeeksforGeeks
In above example, string 1 is Welcome to string 2 is GeeksforGeeks. By using + operator concatenating two string we get output After concatnate two string: Welcome toGeeksforGeeks.
Recommended Posts:
- Scala Iterator concatenation with example
- Scala List concatenation with example
- Scala String indexOf(String str) method with example
- Scala String startsWith(String prefix, int toffset) method with example
- Scala String lastIndexOf(String str, int fromIndex) method with example
- Scala String indexOf(String str, int fromIndex) method with example
- Scala String regionMatches(int toffset, String other, int offset, int len) method with example
- Scala String lastIndexOf(String str) method with example
- Scala String startsWith(String prefix) method with example
- Scala String split(String regex, int limit) method with example
- Scala Tutorial – Learn Scala with Step By Step Guide
- Scala String contentEquals() method with example
- Scala String substring(int beginIndex, int endIndex) method with example
- Scala String
- Program to convert Java Set to a String in Scala
- Scala | String Interpolation
- Scala String subSequence() method with example
- Program to convert Java Set of doubles to a String in Scala
- Program to convert Java Set of shorts to a String in Scala
- Scala String equalsIgnoreCase() method with example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

