Scala - String Methods with Examples - GeeksforGeeks

03 Jul, 2020
In Scala, as in Java, a string is a sequence of characters. In Scala, objects of String are immutable which means a constant and cannot be changed once created. In the rest of this section, we discuss the important methods of java.lang.String class.
char charAt(int index): This method is used to returns the character at the given index.Example:// Scala program of charAt() method   // Creating Object object GFG{          // Main method     def main(args: Array[String]){                  // Getting a character at the given index        // Using charAt() methods        val result = "GeeksforGeeks".charAt(3)        println("Result : " + result)    }}Output:Result : kint compareTo(Object o): This method is used for the comparison of one string to another object.Example:// Scala program of compareTo() method   // Creating Object object GFG{          // Main method     def main(args: Array[String]){                  // Compare two strings        // Using compateTo() methods        val val1 = "Hello"        val val2 = "GeeksforGeeks"        val result = val1.compareTo(val2)        println("Result : " + result)    }}Output:Result : 1int compareTo(String anotherString): This method is used to compares two strings lexicographically. If two strings match then it returns 0, else it returns the difference between the two.Example:// Scala program of compareTo() method   // Creating Object object GFG{          // Main method     def main(args: Array[String]){                  // Compare two strings        // Using compateTo() methods        val result = "Geeks".compareTo("Geeks")        println("Result : " + result)    }}Output:Result : 0int compareToIgnoreCase(String str): This method is used for comparing two strings lexicographically. It ignoring the case differences.Example:// Scala program of compareToIgnoreCase() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using compareToIgnoreCase() methods        val result = "Geeks".compareToIgnoreCase("geeks")        println("Result : " + result)    }}Output:Result : 0
String concat(String str): This method is used for concatenation of the two strings. It join two strings together and made a single string.Example:// Scala program of concat() method   // Creating Object object GFG{          // Main method     def main(args: Array[String]){                  // Concatenate two strings        // Using concat() methods        val result = "Geeks".concat("forGeeks")        println("Result : " + result)    }}Output:Result : GeeksforGeeksBoolean contentEquals(StringBuffer sb): This method is used to compares a string to a StringBuffer’s contents. If they are equal then it returns true otherwise it will return false.Example:// Scala program of contentEquals() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using contentEquals() methods        val a = new StringBuffer("Geeks")        val result = "Geeks".contentEquals(a)        println("Result : " + result)    }}Output:Result : trueBoolean endsWith(String suffix): This method is used to return true if the string ends with the suffix specified. Otherwise, it returns false.Example:// Scala program of endsWith() method     // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using endsWith() methods        val result = "Geeks".endsWith("s")        println("Result : " + result)    }}Output:Result : trueBoolean equals(Object anObject): This method is used to returns true if the string and the object are equal. Otherwise, it returns false.Example:// Scala program of equals() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using equals() methods        val result = "Geeks".equals("Geeks")        println("Result : " + result)    }}Output:Result : trueBoolean equalsIgnoreCase(String anotherString): This methods works like equals() but it ignores the case differences.Example:// Scala program of equalsIgnoreCase() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using equalsIgnoreCase() methods        val result = "Geeks".equalsIgnoreCase("gEeks")        println("Result : " + result)    }}Output:Result : true
byte getBytes(): This method helps in encoding a string into a sequence of bytes and it also helps in storing it into a new byte array.Example:// Scala program of getBytes() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using getBytes() methods        val result = "ABCcba".getBytes()        println("Result : " + result)    }}Output:Result : [B@506e1b77int indexOf(int ch): This method helps in returning the index of the first occurrence of the character in the string.Example:// Scala program of indexOf() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using indexOf() methods        val result = "Geeks".indexOf('e')        println("Result : " + result)    }}Output:Result : 1int indexOf(int ch, int fromIndex): This method works similar to that indexOf. The only difference is that it begins searching at the specified index.Example:// Scala program of indexOf() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using indexOf() methods        val result = "Geeksforgeeks".indexOf('g',5)        println("Result : " + result)    }}Output:Result : 8int indexOf(String str): This method is used to return the index of the first occurrence of the substring we specify, in the string.Example:// Scala program of indexOf() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using indexOf() methods        val result = "Geeksforgeeeks".indexOf("ks")        println("Result : " + result)    }}Output:Result : 3String intern(): This method is used to return the canonical representation for the string object.Example:// Scala program of intern() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using intern() methods        val result = "Geeks,\n\tForGeeks".intern()        println("Result : " + result)    }}Output:Result : Geeks,
    ForGeeks
int lastIndexOf(int ch): This method is used to return the index of the last occurrence of the character we specify.Example:// Scala program of lastIndexOf() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using lastIndexOf() methods        val result = "Geeks".lastIndexOf('e')        println("Result : " + result)    }}Output:Result : 2int lastIndexOf(String str): This method is used to return the index of the last occurrence of the substring we specify, in the string.Example:// Scala program of lastIndexOf() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using lastIndexOf() methods        val result = "Geeksforgeeks".lastIndexOf("ek")        println("Result : " + result)    }}Output:Result : 10int length(): This method is used to return the length of a string.Example:// Scala program of length() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using length() methods        val result = "Geeks".length()        println("Result : " + result)    }}Output:Result : 5String replaceAll(String regex, String replacement): This method is used to replace the substring with the replacement string provided by user.Example:// Scala program of replaceAll() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using replaceAll() methods        val result = "potdotnothotokayslot".replaceAll(".ot","**")        println("Result : " + result)    }}Output:Result : ********okays**String replaceFirst(String regex, String replacement): If in the above example, we want to replace only the first such occurrence.Example:// Scala program of replaceFirst() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using replaceFirst() methods        val result = "potdotnothotokayslot".replaceFirst(".ot","**")        println("Result : " + result)    }}Output:Result : **dotnothotokayslotString[] split(String regex): This method is used to split the string around matches of the regular expression we specify. It returns a String array.Example:// Scala program of split() // method   // Creating object object GfG {       // Main method     def main(args:Array[String])     {               // Applying split method         val result = "PfsoQmsoRcsoGfGkso".split(".so")                   for ( a <-result )         {             // Displays output             println(a)         }               } } Output:P
Q
R
GfG
Boolean startsWith(String prefix, int toffset): This method is used to return true if the string starts with the given index. Otherwise, it will return false.Example:// Scala program of startsWith() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using startsWith() methods        val result = "Geeks".startsWith("ee", 1)        println("Result : " + result)    }}Output:Result : trueCharSequence subSequence(int beginIndex, int endIndex): This method is used to return the sub string from the given string. Here starting index and ending index of a sub string is given.Example:// Scala program of subSequence() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using subSequence() methods        val result = "Geeks".subSequence(1,4)        println("Result : " + result)    }}Output:Result : eekString substring(int beginIndex): This method is used to return the characters of the string beginning from the given index till the end of the string.Example:// Scala program of substring() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using substring() methods        val result = "Geeks".substring(3)        println("Result : " + result)    }}Output:Result : kschar[] toCharArray(): This method is used to convert the string into a CharArray.Example:// Scala program of toCharArray() // method   // Creating object object GfG {       // Main method     def main(args:Array[String])     {               // Applying toCharArray method         val result = "GeeksforGeeks".toCharArray()                   for(m1<-result)         {               // Displays output             println(m1)         }     } } Output:G
e
e
k
s
f
o
r
G
e
e
k
sString toLowerCase(): This method is used to convert all the characters into lower case.Example:// Scala program of toLowerCase() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using toLowerCase() methods        val result = "GEekS".toLowerCase()        println("Result : " + result)    }}Output:Result : geeksString toString(): This method is used to return a String object itself.Example:// Scala program of toString() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using toString() methods        val result = "9".toString()        println("Result : " + result)    }}Output:Result : 9String toUpperCase(): This method is used to convert the string into upper case.Example:// Scala program of toUpperCase() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using toUpperCase() methods        val result = "Geeks".toUpperCase()        println("Result : " + result)    }}Output:Result : GEEKSString trim(): This method is used to remove the leading and trailing whitespaces from the string.Example:// Scala program of trim() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using trim() methods        val result = " Geeks ".trim()        println("Result : " + result)    }}Output:Result : GeeksString substring(int beginIndex, int endIndex): This method is used to return the part of the string beginning at beginIndex and ending at endIndex.Example:// Scala program of substring() // method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using substring() methods        val result = "Piyush".substring(1, 4)        println("Result : " + result)    }}Output:Result : iyuBoolean startsWith(String prefix): This method is used to return true if the string starts with the given prefix. Otherwise, returns false.Example:// Scala program of startsWith() // method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using startsWith() methods        val result = "Ayush".startsWith(" Ay")        println("Result : " + result)    }}Output:String[] split(String regex, int limit): This method is like split, the only change is that we can limit the number of members for the array.Example:// Scala program of split() // method   // Creating object object GfG {       // Main method     def main(args:Array[String])     {               // Applying split method         val result = "NidhifsoSinghmsoAcso".split(".so", 2)               for ( m1 <-result )         {             // Displays output             println(m1)         }       } } Output:Nidhi
SinghmsoAcsoBoolean matches(String regex): This method is used to return true, if string matches the regular expression specified.Example:// Scala program of matches() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using matches() methods        val result = "Ayushi".matches(".i.*")        println("Result : " + result)    }}Output:Result : falseBoolean regionMatches(boolean ignoreCase, int toffset, String other, int offset, int len): This method is used to return true if two strings regions are equal.Example:// Scala program of regionMatches() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using regionMatches() methods        val result = "Ayushi".regionMatches(true, 0, "Ayush", 0, 3)        println("Result : " + result)    }}Output:Result : trueString replace(char oldChar, char newChar): This method is used to replace the oldChar occurences with the newChar ones.Example:// Scala program of replace() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using replace() methods        val result = "sanjay sharma".replace('s','$')        println("Result : " + result)    }}Output:Result : $anjay $harmaint hashCode(): This method is used to return hash code of a string.Example:// Scala program of hashCode() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using hashCode() methods        val result = "Ayushi".hashCode()        println("Result : " + result)    }}Output:Result : 1976240247Boolean regionMatches(int toffset, String other, int offset, int len): This method does not have any ignore case, else it is same as above method.Example:// Scala program of regionMatches() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using regionMatches() methods        val result = "Ayushi".regionMatches(true, 0, "Ayushi",                                            0, 4)        println("Result : " + result)    }}Output:Result : true
char charAt(int index): This method is used to returns the character at the given index.Example:// Scala program of charAt() method   // Creating Object object GFG{          // Main method     def main(args: Array[String]){                  // Getting a character at the given index        // Using charAt() methods        val result = "GeeksforGeeks".charAt(3)        println("Result : " + result)    }}Output:Result : k
Example:
// Scala program of charAt() method   // Creating Object object GFG{          // Main method     def main(args: Array[String]){                  // Getting a character at the given index        // Using charAt() methods        val result = "GeeksforGeeks".charAt(3)        println("Result : " + result)    }}
Output:
Result : k
int compareTo(Object o): This method is used for the comparison of one string to another object.Example:// Scala program of compareTo() method   // Creating Object object GFG{          // Main method     def main(args: Array[String]){                  // Compare two strings        // Using compateTo() methods        val val1 = "Hello"        val val2 = "GeeksforGeeks"        val result = val1.compareTo(val2)        println("Result : " + result)    }}Output:Result : 1
// Scala program of compareTo() method   // Creating Object object GFG{          // Main method     def main(args: Array[String]){                  // Compare two strings        // Using compateTo() methods        val val1 = "Hello"        val val2 = "GeeksforGeeks"        val result = val1.compareTo(val2)        println("Result : " + result)    }}
Output:
Result : 1
int compareTo(String anotherString): This method is used to compares two strings lexicographically. If two strings match then it returns 0, else it returns the difference between the two.Example:// Scala program of compareTo() method   // Creating Object object GFG{          // Main method     def main(args: Array[String]){                  // Compare two strings        // Using compateTo() methods        val result = "Geeks".compareTo("Geeks")        println("Result : " + result)    }}Output:Result : 0
// Scala program of compareTo() method   // Creating Object object GFG{          // Main method     def main(args: Array[String]){                  // Compare two strings        // Using compateTo() methods        val result = "Geeks".compareTo("Geeks")        println("Result : " + result)    }}
Output:
Result : 0
int compareToIgnoreCase(String str): This method is used for comparing two strings lexicographically. It ignoring the case differences.Example:// Scala program of compareToIgnoreCase() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using compareToIgnoreCase() methods        val result = "Geeks".compareToIgnoreCase("geeks")        println("Result : " + result)    }}Output:Result : 0

// Scala program of compareToIgnoreCase() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using compareToIgnoreCase() methods        val result = "Geeks".compareToIgnoreCase("geeks")        println("Result : " + result)    }}
Output:
Result : 0

String concat(String str): This method is used for concatenation of the two strings. It join two strings together and made a single string.Example:// Scala program of concat() method   // Creating Object object GFG{          // Main method     def main(args: Array[String]){                  // Concatenate two strings        // Using concat() methods        val result = "Geeks".concat("forGeeks")        println("Result : " + result)    }}Output:Result : GeeksforGeeks
// Scala program of concat() method   // Creating Object object GFG{          // Main method     def main(args: Array[String]){                  // Concatenate two strings        // Using concat() methods        val result = "Geeks".concat("forGeeks")        println("Result : " + result)    }}
Output:
Result : GeeksforGeeks
Boolean contentEquals(StringBuffer sb): This method is used to compares a string to a StringBuffer’s contents. If they are equal then it returns true otherwise it will return false.Example:// Scala program of contentEquals() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using contentEquals() methods        val a = new StringBuffer("Geeks")        val result = "Geeks".contentEquals(a)        println("Result : " + result)    }}Output:Result : true
// Scala program of contentEquals() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using contentEquals() methods        val a = new StringBuffer("Geeks")        val result = "Geeks".contentEquals(a)        println("Result : " + result)    }}
Output:
Result : true
Boolean endsWith(String suffix): This method is used to return true if the string ends with the suffix specified. Otherwise, it returns false.Example:// Scala program of endsWith() method     // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using endsWith() methods        val result = "Geeks".endsWith("s")        println("Result : " + result)    }}Output:Result : true
// Scala program of endsWith() method     // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using endsWith() methods        val result = "Geeks".endsWith("s")        println("Result : " + result)    }}
Output:
Result : true
Boolean equals(Object anObject): This method is used to returns true if the string and the object are equal. Otherwise, it returns false.Example:// Scala program of equals() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using equals() methods        val result = "Geeks".equals("Geeks")        println("Result : " + result)    }}Output:Result : true
// Scala program of equals() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using equals() methods        val result = "Geeks".equals("Geeks")        println("Result : " + result)    }}
Output:
Result : true
Boolean equalsIgnoreCase(String anotherString): This methods works like equals() but it ignores the case differences.Example:// Scala program of equalsIgnoreCase() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using equalsIgnoreCase() methods        val result = "Geeks".equalsIgnoreCase("gEeks")        println("Result : " + result)    }}Output:Result : true

// Scala program of equalsIgnoreCase() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using equalsIgnoreCase() methods        val result = "Geeks".equalsIgnoreCase("gEeks")        println("Result : " + result)    }}
Output:
Result : true

byte getBytes(): This method helps in encoding a string into a sequence of bytes and it also helps in storing it into a new byte array.Example:// Scala program of getBytes() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using getBytes() methods        val result = "ABCcba".getBytes()        println("Result : " + result)    }}Output:Result : [B@506e1b77
// Scala program of getBytes() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using getBytes() methods        val result = "ABCcba".getBytes()        println("Result : " + result)    }}
Output:
Result : [B@506e1b77
int indexOf(int ch): This method helps in returning the index of the first occurrence of the character in the string.Example:// Scala program of indexOf() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using indexOf() methods        val result = "Geeks".indexOf('e')        println("Result : " + result)    }}Output:Result : 1
// Scala program of indexOf() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using indexOf() methods        val result = "Geeks".indexOf('e')        println("Result : " + result)    }}
Output:
Result : 1
int indexOf(int ch, int fromIndex): This method works similar to that indexOf. The only difference is that it begins searching at the specified index.Example:// Scala program of indexOf() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using indexOf() methods        val result = "Geeksforgeeks".indexOf('g',5)        println("Result : " + result)    }}Output:Result : 8
// Scala program of indexOf() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using indexOf() methods        val result = "Geeksforgeeks".indexOf('g',5)        println("Result : " + result)    }}
Output:
Result : 8
int indexOf(String str): This method is used to return the index of the first occurrence of the substring we specify, in the string.Example:// Scala program of indexOf() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using indexOf() methods        val result = "Geeksforgeeeks".indexOf("ks")        println("Result : " + result)    }}Output:Result : 3
// Scala program of indexOf() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using indexOf() methods        val result = "Geeksforgeeeks".indexOf("ks")        println("Result : " + result)    }}
Output:
Result : 3
String intern(): This method is used to return the canonical representation for the string object.Example:// Scala program of intern() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using intern() methods        val result = "Geeks,\n\tForGeeks".intern()        println("Result : " + result)    }}Output:Result : Geeks,
    ForGeeks

// Scala program of intern() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using intern() methods        val result = "Geeks,\n\tForGeeks".intern()        println("Result : " + result)    }}
Output:
Result : Geeks,
    ForGeeks

int lastIndexOf(int ch): This method is used to return the index of the last occurrence of the character we specify.Example:// Scala program of lastIndexOf() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using lastIndexOf() methods        val result = "Geeks".lastIndexOf('e')        println("Result : " + result)    }}Output:Result : 2
// Scala program of lastIndexOf() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using lastIndexOf() methods        val result = "Geeks".lastIndexOf('e')        println("Result : " + result)    }}
Output:
Result : 2
int lastIndexOf(String str): This method is used to return the index of the last occurrence of the substring we specify, in the string.Example:// Scala program of lastIndexOf() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using lastIndexOf() methods        val result = "Geeksforgeeks".lastIndexOf("ek")        println("Result : " + result)    }}Output:Result : 10
// Scala program of lastIndexOf() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using lastIndexOf() methods        val result = "Geeksforgeeks".lastIndexOf("ek")        println("Result : " + result)    }}
Output:
Result : 10
int length(): This method is used to return the length of a string.Example:// Scala program of length() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using length() methods        val result = "Geeks".length()        println("Result : " + result)    }}Output:Result : 5
// Scala program of length() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using length() methods        val result = "Geeks".length()        println("Result : " + result)    }}
Output:
Result : 5
String replaceAll(String regex, String replacement): This method is used to replace the substring with the replacement string provided by user.Example:// Scala program of replaceAll() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using replaceAll() methods        val result = "potdotnothotokayslot".replaceAll(".ot","**")        println("Result : " + result)    }}Output:Result : ********okays**
// Scala program of replaceAll() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using replaceAll() methods        val result = "potdotnothotokayslot".replaceAll(".ot","**")        println("Result : " + result)    }}
Output:
Result : ********okays**
String replaceFirst(String regex, String replacement): If in the above example, we want to replace only the first such occurrence.Example:// Scala program of replaceFirst() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using replaceFirst() methods        val result = "potdotnothotokayslot".replaceFirst(".ot","**")        println("Result : " + result)    }}Output:Result : **dotnothotokayslot
// Scala program of replaceFirst() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using replaceFirst() methods        val result = "potdotnothotokayslot".replaceFirst(".ot","**")        println("Result : " + result)    }}
Output:
Result : **dotnothotokayslot
String[] split(String regex): This method is used to split the string around matches of the regular expression we specify. It returns a String array.Example:// Scala program of split() // method   // Creating object object GfG {       // Main method     def main(args:Array[String])     {               // Applying split method         val result = "PfsoQmsoRcsoGfGkso".split(".so")                   for ( a <-result )         {             // Displays output             println(a)         }               } } Output:P
Q
R
GfG

// Scala program of split() // method   // Creating object object GfG {       // Main method     def main(args:Array[String])     {               // Applying split method         val result = "PfsoQmsoRcsoGfGkso".split(".so")                   for ( a <-result )         {             // Displays output             println(a)         }               } } 
Output:
P
Q
R
GfG

Boolean startsWith(String prefix, int toffset): This method is used to return true if the string starts with the given index. Otherwise, it will return false.Example:// Scala program of startsWith() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using startsWith() methods        val result = "Geeks".startsWith("ee", 1)        println("Result : " + result)    }}Output:Result : true
// Scala program of startsWith() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using startsWith() methods        val result = "Geeks".startsWith("ee", 1)        println("Result : " + result)    }}
Output:
Result : true
CharSequence subSequence(int beginIndex, int endIndex): This method is used to return the sub string from the given string. Here starting index and ending index of a sub string is given.Example:// Scala program of subSequence() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using subSequence() methods        val result = "Geeks".subSequence(1,4)        println("Result : " + result)    }}Output:Result : eek
// Scala program of subSequence() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using subSequence() methods        val result = "Geeks".subSequence(1,4)        println("Result : " + result)    }}
Output:
Result : eek
String substring(int beginIndex): This method is used to return the characters of the string beginning from the given index till the end of the string.Example:// Scala program of substring() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using substring() methods        val result = "Geeks".substring(3)        println("Result : " + result)    }}Output:Result : ks
// Scala program of substring() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using substring() methods        val result = "Geeks".substring(3)        println("Result : " + result)    }}
Output:
Result : ks
char[] toCharArray(): This method is used to convert the string into a CharArray.Example:// Scala program of toCharArray() // method   // Creating object object GfG {       // Main method     def main(args:Array[String])     {               // Applying toCharArray method         val result = "GeeksforGeeks".toCharArray()                   for(m1<-result)         {               // Displays output             println(m1)         }     } } Output:G
e
e
k
s
f
o
r
G
e
e
k
s
// Scala program of toCharArray() // method   // Creating object object GfG {       // Main method     def main(args:Array[String])     {               // Applying toCharArray method         val result = "GeeksforGeeks".toCharArray()                   for(m1<-result)         {               // Displays output             println(m1)         }     } } 
Output:
G
e
e
k
s
f
o
r
G
e
e
k
s
String toLowerCase(): This method is used to convert all the characters into lower case.Example:// Scala program of toLowerCase() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using toLowerCase() methods        val result = "GEekS".toLowerCase()        println("Result : " + result)    }}Output:Result : geeks
// Scala program of toLowerCase() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using toLowerCase() methods        val result = "GEekS".toLowerCase()        println("Result : " + result)    }}
Output:
Result : geeks
String toString(): This method is used to return a String object itself.Example:// Scala program of toString() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using toString() methods        val result = "9".toString()        println("Result : " + result)    }}Output:Result : 9
// Scala program of toString() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using toString() methods        val result = "9".toString()        println("Result : " + result)    }}
Output:
Result : 9
String toUpperCase(): This method is used to convert the string into upper case.Example:// Scala program of toUpperCase() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using toUpperCase() methods        val result = "Geeks".toUpperCase()        println("Result : " + result)    }}Output:Result : GEEKS
// Scala program of toUpperCase() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using toUpperCase() methods        val result = "Geeks".toUpperCase()        println("Result : " + result)    }}
Output:
Result : GEEKS
String trim(): This method is used to remove the leading and trailing whitespaces from the string.Example:// Scala program of trim() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using trim() methods        val result = " Geeks ".trim()        println("Result : " + result)    }}Output:Result : Geeks
// Scala program of trim() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using trim() methods        val result = " Geeks ".trim()        println("Result : " + result)    }}
Output:
Result : Geeks
String substring(int beginIndex, int endIndex): This method is used to return the part of the string beginning at beginIndex and ending at endIndex.Example:// Scala program of substring() // method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using substring() methods        val result = "Piyush".substring(1, 4)        println("Result : " + result)    }}Output:Result : iyu
// Scala program of substring() // method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using substring() methods        val result = "Piyush".substring(1, 4)        println("Result : " + result)    }}
Output:
Result : iyu
Boolean startsWith(String prefix): This method is used to return true if the string starts with the given prefix. Otherwise, returns false.Example:// Scala program of startsWith() // method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using startsWith() methods        val result = "Ayush".startsWith(" Ay")        println("Result : " + result)    }}Output:
// Scala program of startsWith() // method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using startsWith() methods        val result = "Ayush".startsWith(" Ay")        println("Result : " + result)    }}
Output:
String[] split(String regex, int limit): This method is like split, the only change is that we can limit the number of members for the array.Example:// Scala program of split() // method   // Creating object object GfG {       // Main method     def main(args:Array[String])     {               // Applying split method         val result = "NidhifsoSinghmsoAcso".split(".so", 2)               for ( m1 <-result )         {             // Displays output             println(m1)         }       } } Output:Nidhi
SinghmsoAcso
// Scala program of split() // method   // Creating object object GfG {       // Main method     def main(args:Array[String])     {               // Applying split method         val result = "NidhifsoSinghmsoAcso".split(".so", 2)               for ( m1 <-result )         {             // Displays output             println(m1)         }       } } 
Output:
Nidhi
SinghmsoAcso
Boolean matches(String regex): This method is used to return true, if string matches the regular expression specified.Example:// Scala program of matches() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using matches() methods        val result = "Ayushi".matches(".i.*")        println("Result : " + result)    }}Output:Result : false
// Scala program of matches() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using matches() methods        val result = "Ayushi".matches(".i.*")        println("Result : " + result)    }}
Output:
Result : false
Boolean regionMatches(boolean ignoreCase, int toffset, String other, int offset, int len): This method is used to return true if two strings regions are equal.Example:// Scala program of regionMatches() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using regionMatches() methods        val result = "Ayushi".regionMatches(true, 0, "Ayush", 0, 3)        println("Result : " + result)    }}Output:Result : true
// Scala program of regionMatches() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using regionMatches() methods        val result = "Ayushi".regionMatches(true, 0, "Ayush", 0, 3)        println("Result : " + result)    }}
Output:
Result : true
String replace(char oldChar, char newChar): This method is used to replace the oldChar occurences with the newChar ones.Example:// Scala program of replace() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using replace() methods        val result = "sanjay sharma".replace('s','$')        println("Result : " + result)    }}Output:Result : $anjay $harma
// Scala program of replace() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using replace() methods        val result = "sanjay sharma".replace('s','$')        println("Result : " + result)    }}
Output:
Result : $anjay $harma
int hashCode(): This method is used to return hash code of a string.Example:// Scala program of hashCode() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using hashCode() methods        val result = "Ayushi".hashCode()        println("Result : " + result)    }}Output:Result : 1976240247
// Scala program of hashCode() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using hashCode() methods        val result = "Ayushi".hashCode()        println("Result : " + result)    }}
Output:
Result : 1976240247
Boolean regionMatches(int toffset, String other, int offset, int len): This method does not have any ignore case, else it is same as above method.Example:// Scala program of regionMatches() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using regionMatches() methods        val result = "Ayushi".regionMatches(true, 0, "Ayushi",                                            0, 4)        println("Result : " + result)    }}Output:Result : true
// Scala program of regionMatches() method   // Creating Object object GFG{          // Main method     def main(args: Array[String])    {                  // Using regionMatches() methods        val result = "Ayushi".regionMatches(true, 0, "Ayushi",                                            0, 4)        println("Result : " + result)    }}
Output:
Result : true
Scala-Strings
Scala
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Scala List filter() method with example
Type Casting in Scala
Class and Object in Scala
Scala Map
Scala Lists
Inheritance in Scala
Scala Tutorial – Learn Scala with Step By Step Guide
Scala List contains() method with example
Operators in Scala