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.
Scala
object GFG
{
def main(args : Array[String]){
val result = "w3wiki".charAt( 3 )
println("Result : " + result)
}
}
|
- int compareTo(Object o): This method is used for the comparison of one string to another object.
Scala
object GFG
{
def main(args : Array[String]){
val val 1 = "Hello"
val val 2 = "w3wiki"
val result = val 1 .compareTo(val 2 )
println("Result : " + result)
}
}
|
- 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.
Scala
object GFG
{
def main(args : Array[String]){
val result = "Beginner".compareTo("Beginner")
println("Result : " + result)
}
}
|
- int compareToIgnoreCase(String str): This method is used for comparing two strings lexicographically. It ignoring the case differences.
Scala
object GFG
{
def main(args : Array[String])
{
val result = "Beginner".compareToIgnoreCase("Beginner")
println("Result : " + result)
}
}
|
- String concat(String str): This method is used for concatenation of the two strings. It join two strings together and made a single string.
Scala
object GFG
{
def main(args : Array[String]){
val result = "Beginner".concat("forBeginner")
println("Result : " + result)
}
}
|
- 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.
Scala
object GFG
{
def main(args : Array[String])
{
val a = new StringBuffer("Beginner")
val result = "Beginner".contentEquals(a)
println("Result : " + result)
}
}
|
- Boolean endsWith(String suffix): This method is used to return true if the string ends with the suffix specified. Otherwise, it returns false.
Scala
object GFG
{
def main(args : Array[String])
{
val result = "Beginner".endsWith("s")
println("Result : " + result)
}
}
|
- Boolean equals(Object anObject): This method is used to returns true if the string and the object are equal. Otherwise, it returns false.
Scala
object GFG
{
def main(args : Array[String])
{
val result = "Beginner".equals("Beginner")
println("Result : " + result)
}
}
|
- Boolean equalsIgnoreCase(String anotherString): This methods works like equals() but it ignores the case differences.
Scala
object GFG
{
def main(args : Array[String])
{
val result = "Beginner".equalsIgnoreCase("gEeks")
println("Result : " + result)
}
}
|
- 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.
Scala
object GFG
{
def main(args : Array[String])
{
val result = "ABCcba".getBytes()
println("Result : " + result)
}
}
|
- int indexOf(int ch): This method helps in returning the index of the first occurrence of the character in the string.
Scala
object GFG
{
def main(args : Array[String])
{
val result = "Beginner".indexOf( 'e' )
println("Result : " + result)
}
}
|
- 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.
Scala
object GFG
{
def main(args : Array[String])
{
val result = "w3wiki".indexOf( 'g' , 5 )
println("Result : " + result)
}
}
|
- int indexOf(String str): This method is used to return the index of the first occurrence of the substring we specify, in the string.
Scala
object GFG
{
def main(args : Array[String])
{
val result = "Beginnerforgeeeks".indexOf("ks")
println("Result : " + result)
}
}
|
- String intern(): This method is used to return the canonical representation for the string object.
Scala
object GFG
{
def main(args : Array[String])
{
val result = "Beginner,\n\tForBeginner".intern()
println("Result : " + result)
}
}
|
Result : Beginner,
ForBeginner
- int lastIndexOf(int ch): This method is used to return the index of the last occurrence of the character we specify.
Scala
object GFG
{
def main(args : Array[String])
{
val result = "Beginner".lastIndexOf( 'e' )
println("Result : " + result)
}
}
|
- int lastIndexOf(String str): This method is used to return the index of the last occurrence of the substring we specify, in the string.
Scala
object GFG
{
def main(args : Array[String])
{
val result = "w3wiki".lastIndexOf("ek")
println("Result : " + result)
}
}
|
- int length(): This method is used to return the length of a string.
Scala
object GFG
{
def main(args : Array[String])
{
val result = "Beginner".length()
println("Result : " + result)
}
}
|
- String replaceAll(String regex, String replacement): This method is used to replace the substring with the replacement string provided by user.
Scala
object GFG
{
def main(args : Array[String])
{
val result = "potdotnothotokayslot".replaceAll(".ot","**")
println("Result : " + result)
}
}
|
- String replaceFirst(String regex, String replacement): If in the above example, we want to replace only the first such occurrence.
Scala
object GFG
{
def main(args : Array[String])
{
val result = "potdotnothotokayslot".replaceFirst(".ot","**")
println("Result : " + result)
}
}
|
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.
Scala
object GfG
{
def main(args : Array[String])
{
val result = "PfsoQmsoRcsoGfGkso".split(".so")
for ( a <-result )
{
println(a)
}
}
}
|
- 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
object GFG
{
def main(args : Array[String])
{
val result = "Beginner".startsWith("ee", 1 )
println("Result : " + result)
}
}
|
- 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.
Scala
object GFG
{
def main(args : Array[String])
{
val result = "Beginner".subSequence( 1 , 4 )
println("Result : " + result)
}
}
|
- 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.
Scala
object GFG
{
def main(args : Array[String])
{
val result = "Beginner".substring( 3 )
println("Result : " + result)
}
}
|
- char[] toCharArray(): This method is used to convert the string into a CharArray.
Scala
object GfG
{
def main(args : Array[String])
{
val result = "w3wiki".toCharArray()
for (m 1 <-result)
{
println(m 1 )
}
}
}
|
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.
Scala
object GFG
{
def main(args : Array[String])
{
val result = "GEekS".toLowerCase()
println("Result : " + result)
}
}
|
- String toString(): This method is used to return a String object itself.
Scala
object GFG
{
def main(args : Array[String])
{
val result = " 9 ".toString()
println("Result : " + result)
}
}
|
- String toUpperCase(): This method is used to convert the string into upper case.
Scala
object GFG
{
def main(args : Array[String])
{
val result = "Beginner".toUpperCase()
println("Result : " + result)
}
}
|
- String trim(): This method is used to remove the leading and trailing whitespaces from the string.
Scala
object GFG
{
def main(args : Array[String])
{
val result = " Beginner ".trim()
println("Result : " + result)
}
}
|
- 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
object GFG
{
def main(args : Array[String])
{
val result = "Piyush".substring( 1 , 4 )
println("Result : " + result)
}
}
|
- Boolean startsWith(String prefix): This method is used to return true if the string starts with the given prefix. Otherwise, returns false.
Scala
object GFG
{
def main(args : Array[String])
{
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.
Scala
object GfG
{
def main(args : Array[String])
{
val result = "NidhifsoSinghmsoAcso".split(".so", 2 )
for ( m 1 <-result )
{
println(m 1 )
}
}
}
|
- Boolean matches(String regex): This method is used to return true, if string matches the regular expression specified.
Scala
object GFG
{
def main(args : Array[String])
{
val result = "Ayushi".matches(".i.*")
println("Result : " + result)
}
}
|
- 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
object GFG
{
def main(args : Array[String])
{
val result = "Ayushi".regionMatches( true , 0 , "Ayush", 0 , 3 )
println("Result : " + result)
}
}
|
- String replace(char oldChar, char newChar): This method is used to replace the oldChar occurrences with the newChar ones.
Scala
object GFG
{
def main(args : Array[String])
{
val result = "sanjay sharma".replace( 's' , '$' )
println("Result : " + result)
}
}
|
- int hashCode(): This method is used to return hash code of a string.
Scala
object GFG
{
def main(args : Array[String])
{
val result = "Ayushi".hashCode()
println("Result : " + result)
}
}
|
- 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.
Scala
object GFG
{
def main(args : Array[String])
{
val result = "Ayushi".regionMatches( true , 0 , "Ayushi",
0 , 4 )
println("Result : " + result)
}
}
|