...

/

Replacing Patterns in Strings

Replacing Patterns in Strings

In the following lesson, you will learn how to replace patterns in a string.

Problem

Imagine a situation where you not only want to find an expression, as we did in the previous lesson, but also want to replace it with another expression. For instance, let’s say you’re filling out an online form and where it says name, you’ll need to replace it with your actual name.

Solution

One thing you need to know before we get into the solution is that strings are immutable, i.e., they cannot be modified. To overcome this obstacle, we create a new string which contains the replaced expression rather than modifying the old one.

The built-in Scala methods we will discuss below, automatically create a new string, so you don’t have to. ...

Replace the First