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”.

str.length

It returns the length of the string in characters.

scala> str.length 
res1: Int = 8

str.substring(i)

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

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

str.substring(i,j)

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

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

str.slice(i,j)

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

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

str.contains(x)

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

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

str.indexOf(x)

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

str.toLowerCase

Converts string into lower case.

scala> str.toLowerCase
res10: String = employee

str.toUpperCase

Converts string into uppercase.

scala> str.toUpperCase
res11: String = EMPLOYEE

str.capitalize

returns a string with first character as upper case.

scala> str.capitalize
res13: String = Employee

str.isEmpty

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

scala> str.isEmpty
res15: Boolean = false

str.startsWith(x)

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

str.endsWith(x)

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

str.reverse

returns reversed string.

scala> str.reverse
res20: String = eeyolpmE

str.replace(x,y)

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

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

str.trim

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

scala> str.trim
res24: String = Employee

str.split(delimiter)

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 testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...