Basic String Operations and Interpolation in Scala

Reading Time: 3 minutes

In this blog post, we will talk about strings and some basic string operations. Then, we will discuss string interpolation in Scala.

Strings in Scala

What are Strings ?

A string in Scala is a collection of characters. In Scala, string objects are immutable, meaning they can not be changed after they have been created.

val myString: String = "This is a blog"

Output :- This is a blog

What are string operations ?

Task performed on a string to get any specific value is called string operations.
For example: Computing length of a string is a string operation.

We mainly break down a complex string problem into small string operations or tasks, such as determining the length of a string, comparing two strings, or searching for a specific character in a string, and these are the required string operations.

Some basic string operations are :

  • charAt()
  • length
  • startsWith()
  • replace()
  • toLowerCase()

1- char charAt( Int index ) :

This method returns the character at the specified index.

object StringOperations extends App{
  val str: String = "Hello"

Output :- l

2- int length()

This method returns the length of a string.

object StringOperations extends App{
  val str1: String = "Hello"

Output :- 5

3- String toLowerCase(): 

This method converts all of the characters to lower case.

object StringOperations extends App{
  val str2 = "This is my first blog."

Output :- this is my first blog.

4- Boolean startsWith(String prefix, int toffset) :

This method returns true if the string begins with the substring at the given index. If not, it will return false.

object StringOperations extends App{ 
  val str3 = "Helloall"

Output :- true

5- String replace(char oldChar, char newChar): 

This method replaces all the oldChar occurrences with newChar occurrences.

object StringOperations extends App{ 
  val str4 = "He has an apple"

Output :- He h@s @n @pple

Let's take a look at a code snippet that demonstrates string operations.

Basic string operations

String Interpolation :

String interpolation is a new mechanism in Scala that allows us to create strings from your data and it enables users to directly embed variable references in processed string literals.

To use this Scala functionality, we must adhere to a few guidelines:

  • String must be defined with starting character as s / f /raw
  • Variables in the String must have ‘$’ as prefix.
  • Expressions must be enclosed in curly braces (, ) and a prefix of ‘$’ must be used.

Types of String Interpolators :

1) s – Interpolator :

We can access variables, object fields and function calls within the String. Variables can be used directly in the string by prepending ‘s’ to any string literal.

object StringInterpolation extends App{
  val name = "Pragati"
  println(s"Hello, My name is $name ")
  println(s"3 * 5 = ${3*5}")

Output :- Hello, My name is Pragati
          3 * 5 = 15

2) f – Interpolator :

Similar to printf in other languages, appending f to any string literal allows you to create basic formatted strings.This interpolation aids in the easy formatting of numbers. 

object StringInterpolation extends App{
  val speed = 50.678
  println(f"He is driving at $speed%2.2f km/hr")

Output :- He is driving at 50.68 km/hr

3) raw Interpolator :

The raw interpolator is similar to the s interpolator, with the exception that it does not do any literal escaping within the string.

object StringInterpolation extends App{
  val str = raw"New\n Blog Post"

Output :- New\n Blog Post

Let's take a look at a code snippet that demonstrates string interpolation

String interpolation in scala


So, in this blog post, we covered some fundamental string operations. Then, we learned about string interpolation in Scala and its various types.


Written by 

Pragati is currently working as a Software Engineer at Knoldus Inc. She is having more than 1.5 year of experience working in IT industry and working in the Scala domain.