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 Tutorial – Learn Scala with Step By Step Guide
- 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 String contentEquals() method with example
- Scala String substring(int beginIndex, int endIndex) method with example
- 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
- String concatenation in Scala
- Scala String equalsIgnoreCase() method with example
- Scala String endsWith() method with example
- Scala String compareToIgnoreCase() 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 email@example.com. 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.