Scala String

18 Aug, 2021
A string is a sequence of characters. In Scala, objects of String are immutable which means a constant and cannot be changed once created.  
There are two ways to create a string in Scala: 
Here, when the compiler meet to a string literal and creates a string object str. Syntax: 
var str = "Hello! GFG"
or
val str = "Hello! GFG"
Here, a String type is specified before meeting the string literal. Syntax: 
var str: String = "Hello! GFG"
or
val str: String = "Hello! GFG"
Note: If you need to append to the original string, then use StringBuilder class. Example: 
Scala
// Scala program to illustrate how to // create a stringobject Main{          // str1 and str2 are two different strings    var str1 = "Hello! GFG"    val str2: String = "GeeksforGeeks"    def main(args: Array[String])     {                  // Display both strings        println(str1);        println(str2);    }}
Output:  
Hello! GFG
GeeksforGeeks
An accessor method is those methods which are used to find the information about the object. So, a length() method is the accessor method in Scala, which is used to find the length of the given string. Or in other words, length() method returns the number of characters that are present in the string object. Syntax:  
var len1 = str1.length();
Example: 
Scala
// Scala program to illustrate how to // get the length of the given stringobject Main {          // str1 and str2 are two strings    var str1 = "Hello! GFG"    var str2: String = "GeeksforGeeks"          // Main function    def main(args: Array[String])     {                  // Get the length of str1 and str2 strings        // using length() function        var LEN1 = str1.length();        var LEN2 = str2.length();                  // Display both strings with their length        println("String 1:" + str1 + ", Length :" + LEN1);        println("String 2:" + str2 + ", Length :" + LEN2);    }}
Output:  
String 1:Hello! GFG, Length :10
String 2:GeeksforGeeks, Length :13
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. You can also use ‘+’ operator to concatenate two strings. Syntax:  
str1.concat(str2);
or Syntax:  
"welcome" + "GFG"
Example: 
Scala
// Scala program to illustrate how to // concatenate stringsobject Main {          // 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
When you required format number or values in your string you will use printf() or format() methods. Other than these methods, String class also provides a methods named as format() method, this method return a String object instead of PrintStream object. Example: 
Scala
// Scala program to illustrate how to // Creating format stringobject Main {          // two strings    var A_name = "Ankita "    var Ar_name = "Scala|Strings"    var total = 130          // Main function    def main(args: Array[String])     {                  // using format() function        println("%s, %s, %d".format(A_name, Ar_name, total));    }}
Output: 
Ankita , Scala|Strings, 130
kapoorsagar226
Scala
Scala-Strings
Scala
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.