Skip to content
Related Articles

Related Articles

Scala String
  • Last Updated : 25 Jan, 2019

A string is a sequence of characters. In Scala, objects of String are immutable which means a constant and cannot be changed once created.

Creating a String in Scala

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.
    Synatx:
    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 program to illustrate how to 
// create a string
object 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

Get length of the string

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 program to illustrate how to 
// get the length of the given string
object 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

Concatenating strings in Scala

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 program to illustrate how to 
// concatenate strings
object 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

Creating format string

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 program to illustrate how to 
// Creating format string
object 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

Some important string functions

FunctionDescription
char charAt(int index)This function returns the character at the given index.
String replace(char ch1, char ch2)This function returns a new string in which the element of ch1 is replaced by the ch2.
String[] split(String reg)This function splits the string around matches of the given regular expression.
String substring(int i)This function returns a new string that is a substring of the given string.
String trim()This function returns a copy of the string, with starting and ending whitespace removed.
boolean startsWith(String prefix)This function is used to check if the given string starts with the specified prefix or not.
My Personal Notes arrow_drop_up
Recommended Articles
Page :