Title: Replacing Parts Of Strings
Slug: replacing_parts_of_strings
Summary: Replacing Parts Of Strings Using Scala.
Date: 2017-01-03 12:00
Category: Scala
Tags: Basics
Authors: Chris Albon
This tutorial was inspired by the awesome Scala Cookbook.
In [1]:
// Create a string value
val text: String = "Lt. Steve Miller will be leading the attack."
In [2]:
// Create a regex pattern for a name
val find_steve = "Steve Miller".r
In [3]:
// Replace all instances of the pattern with a different name
find_steve.replaceAllIn(text, "Peter Jackson")
Out[3]:
In [7]:
// Replace first instance of the pattern with a different name
find_steve.replaceFirstIn(text, "Peter Jackson")
Out[7]: