Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions chapters/strings/replacing-sub-strings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
layout: recipe
title: Replacing Sub-Strings Within a String
chapter: Strings
---
## Problem

You want to replace a sub-string with a new sub-string.

## Solution

Split the string using the sub-string you want to remove as a delimiter. Then re-join using the new sub-string as the delimiter.

{% highlight coffeescript %}
"Orange is the new Black".split("Orange").join("Pink")
# => "Pink is the new Black"

"I am so sad. I cannot believe how sad I am today!".split("sad").join("happy")
# => "I am so happy. I cannot believe how happy I am today!"

"I am not a crook.".split("not ").join("")
# => "I am a crook."
{% endhighlight %}

## Discussion

You can also use regexes. If you're matching an exact string, this way is simpler and 10x faster.

If you use regexes, remember that you must escape certain characters.