Scala – String

Scala string is a sequence of characters inside quotes. It is an immutable object like in java that means its values can not be modified. When you write a String in REPL (Read-Evaluate-Print-Loop) environment, and get the name of the String literal, it will print java.lang.String which tells us that scala uses java String class, hence allows us to use its methods in scala.

String in Scala prints java.lang.String

As you can see in the above image, we have defined the type as String while declaring variable str. What if we do not mention the type and write the String as given below?

val str = "Employee"

It will also work , as compiler understands it as String literal and creates a string object str. We can see an example how it is an immutable object. If we replace the characters in the string , it gives a new string and original string str remains intact. We are replacing “Emp” with “123” ,and it gives a new string. But String str remains as Employee when printed again.

scala> str
res26: String = Employee

scala> str.replace("Emp","123")
res27: String = 123loyee

scala> str
res28: String = Employee

Now, let’s see some important methods of string.

Consider that we have a string object as – val str = “Employee”.


It returns the length of the string in characters.

scala> str.length 
res1: Int = 8


It returns the part of string staring at index i. Index start from 0.

scala> str.substring(2)
res2: String = ployee


It returns the part of String starting at index i and goes till index (j-1).

scala> str.substring(2,5)
res4: String = plo


It also works the same as substring and gives the same result.

scala> str.substring(2,5)
res4: String = plo


It returns boolean value as true if x is substring of str.

scala> str.contains("yee")
res6: Boolean = true


It returns the index of first occurrence of substring in string str or -1 if not present.

scala> str.indexOf("yee")
res8: Int = 5
scala> str.indexOf("ayee")
res9: Int = -1


Converts string into lower case.

scala> str.toLowerCase
res10: String = employee


Converts string into uppercase.

scala> str.toUpperCase
res11: String = EMPLOYEE


returns a string with first character as upper case.

scala> str.capitalize
res13: String = Employee


returns boolean value true if string object is empty else false. It is same like str.length==0.

scala> str.isEmpty
res15: Boolean = false


returns true if str starts with x. It is case sensitive.

scala> str.startsWith("E")
res16: Boolean = true

scala> str.startsWith("e")
res17: Boolean = false


returns true if str ends with x. It is case sensitive.

scala> str.endsWith("e")
res18: Boolean = true

scala> str.endsWith("E")
res19: Boolean = false


returns reversed string.

scala> str.reverse
res20: String = eeyolpmE


It returns a new string with all characters x replaced by y.

scala> str.replace("ee","123")
res23: String = Employ123


It returns a string with white spaces removed both the side.

scala> str.trim
res24: String = Employee


It splits the string on the basis of delimiter and returns a array of strings.

scala> str.split("l")
res25: Array[String] = Array(Emp, oyee)
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...