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.
var str = "Hello! GFG" or val str = "Hello! GFG"
- Here, a String type is specified before meeting the string literal.
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.
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.
var len1 = str1.length();
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.
"welcome" + "GFG"
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.
Ankita , Scala|Strings, 130
Some important string functions
|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.|
- Scala | String Interpolation
- String concatenation in Scala
- Set in Scala | Set-1
- Scala Map
- Set in Scala | Set-2
- Scala | Either
- Operators in Scala
- Scala | ArrayBuffer
- How to get all the keys from a Scala map
- Scala | REPL
- Variables in Scala
- Scala Varargs
- How to get all the values from a Scala map
- Scala ListBuffer
- StringBuilder in Scala
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.