Scala: Strings

Learn
2 min readJan 12, 2022

--

Working with strings is something that a programmer needs to deal with on a regular basis. Scala provides some capabilities in regards to working with strings over and above what Java provides. This post is to explore some of these.

String as a sequence of characters
Scala strings can be worked with as a sequence of characters, which feels a bit more intuitive to work with. For example, to get a character at a particular position, it feels nicer to access it as word(2) instead of word.charAt(3)

val s:String = "abcde"
println(s(0))
a

Collection api power to strings
This also implies that you have all the standard collection functionalities of map , foreach , filter etc on the String.

Map function

val s:String = "cityofchicago"
val caps:String = s.map(Character.toUpperCase(_))
CITYOFCHICAGO

You can use the map function to map each character to it’s upper case as demonstrated above.

Filter function
You can filter specific characters for the sequence of characters that a string is as shown below.

val s:String = "cityofchicago"
val res:String = s.filter(_ == 'c')
println(res)
ccc

Regular expressions
Another typical use case with Strings is the need to find matching patterns in a string, or replacing particular matched patterns with other values.

Scala has a syntactically convenient .r method to create RegularExpression instances, which can then be used to match and replace expressions.

val lowercRegex = "c".r
val matches = lowercRegex.findFirstIn("cityOFchicago")
println(matches)
Some(c)

findFirstIn/ findAllIn methods return iterators that can be worked with as below

val lowercRegex = "c".r
lowercRegex.findAllIn("cityOFchicago")
.foreach(println)
c
c
c
lowercRegex.findFirstIn("cityOFchicago")
.foreach(println)
c

Multiline strings in scala can be written using triple quotes

val s = """Line one
Line two
Line three"""
println(s)Line one
Line two
Line three

Sometimes, the output needs to be a long single line string but you do not want to write such a long line of static text in your code, then you can do the following

val s = """Line one
Line two
Line three""".replaceAll("\n","")
println(s)Line oneLine twoLine three

Implicits
Scala support Implicits which is a mechanism to add additional functionality to a class without directly changing it. You can leverage the same mechanics while working with Strings.

For instance, you might have a use-case to add hiphens between all letters in your string, you can define an implicit class against the String class, add the functionality and then directly run it on your strings

implicit class CustomString(s : String) {
def increment() = s.map(c => (c+1).toChar)
}
val cs = CustomString("abc")
println(cs.increment())
println(cs.increment().increment().increment())
println("ABC".increment())bcd
def
BCD

This example demonstrates the addition of an increment method and how this can be easily applied directly on the string instance itself.

--

--

Learn
Learn

Written by Learn

On a continuing learning journey..

No responses yet