Scala String
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.
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 string object Main { // str1 and str2 are two different strings var str 1 = "Hello! GFG" val str 2 : String = "GeeksforGeeks" def main(args : Array[String]) { // Display both strings println(str 1 ); println(str 2 ); } } |
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
// Scala program to illustrate how to // get the length of the given string object Main { // str1 and str2 are two strings var str 1 = "Hello! GFG" var str 2 : String = "GeeksforGeeks" // Main function def main(args : Array[String]) { // Get the length of str1 and str2 strings // using length() function var LEN 1 = str 1 .length(); var LEN 2 = str 2 .length(); // Display both strings with their length println( "String 1:" + str 1 + ", Length :" + LEN 1 ); println( "String 2:" + str 2 + ", Length :" + LEN 2 ); } } |
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
// Scala program to illustrate how to // concatenate strings object Main { // str1 and str2 are two strings var str 1 = "Welcome! GeeksforGeeks " var str 2 = " to Portal" // Main function def main(args : Array[String]) { // concatenate str1 and str2 strings // using concat() function var Newstr = str 1 .concat(str 2 ); // Display strings println( "String 1:" +str 1 ); println( "String 2:" +str 2 ); 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
// 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
Function | Description |
---|---|
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. |