Different methods are used in string operation – Scala

Reading Time: 3 minutes

Scala Strings are one of the most important features of scala. Basically, it is a sequence of characters that uses the linear approach to store data. In scala, the string is immutable in nature and hence we cannot change its original state. Scala language provides us various methods to play with strings we can use these methods on strings to get the desired output. If you want to know about these methods then this blog is for you.

scala string

Scala string method for splitting a string

split

In Scala, we can use the split method to split strings into an array of strings. The split method uses specific separators such as commas to separate strings. Let’s see how the split method work using the below example.

Scala string split

The output of the above code is

Scala string split output

Scala string methods for Comparing Strings

There are various built-in methods in scala for string comparison like matches, equalsIgnoreCase, equals. Let us discuss what are these methods and how they work.

matches

This built-in method is used to match regular expressions with an entire string. It returns true if the string matches and false if it doesn’t match. Let’s see how the matches method work using the below example.

Scala string matches

The output of the above code is

matches output

equals

The equals the method work similarly to ‘==’. It returns true when both values are identical and returns false when both values are not identical.Let’s see how the equals method work using the below example.

  equals

The output of the above code is

  equal output

equalsIgnoreCase

The equalsIgnoreCase the method work similarly to equals method. The only difference between these methods is that equalsIgnoreCase is ignoring case sensitivity. It means for equalsIgnoreCase “a” and “A” are the same. Let’s see how the equalsIgnoreCase method work using the below example.

 equalsIgnoreCase Scala String

The output of the above code is

 equalsIgnoreCase output

Scala string method for replacing Patterns in Strings

replaceFirst

The replaceFirst method is used to replace the first matching occurrence of an expression in a string. Let’s see how the replaceFirst method work using the below example.

Scala String

The output of the above code is

Scala String

replaceAll

The replaceAll method is used to replace all matching occurrences of an expression in a string. Let’s see how the replaceFirst method work using the below example.

Scala String

The output of the above code is

Scala String

Conclusion

In this blog, we get to know about various methods in scala which we can perform on a string to get desired output.

Written by 

Divyansh Devrani is a Software Consultant having experience in the Scala Ecosystem. He has worked on technologies like Scala, Akka, Play, and Postgre. He is recognized as a good team player who wants to interact with new technologies. He loves to learn new things His hobbies include listening to muisc, watching movies and anime.