# String replace

**URL:** <https://www.ruby-forum.com/t/string-replace/174707>\
**Category:** Ruby\
**Created:** [August 31, 2009, 6:35pm UTC](https://www.ruby-forum.com/t/string-replace/174707 "2009-08-31T18:35:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [August 31, 2009, 6:35pm UTC](https://www.ruby-forum.com/t/string-replace/174707/1 "2009-08-31T18:35:36Z")

</div>

I wondering if there is a string replace function.

I have a string that looks something like:

2YP/P:[CTYCODE]B/[W\_PHONE]/F/[W\_FAX]/R/[H\_PHONE]

and what I am looking to do is to replace the [W\_PHONE], for example,  
with 555-555-5555. What I have right now in terms of code is something  
like:

if /[W\_PHONE]/ =~ 2YP/P:[CTYCODE]B/[W\_PHONE]/F/[W\_FAX]/R/[H\_PHONE]  
@var = "555-555-5555  
end

which works but it builds a new string, which is fine, but the above  
string could be constructed any number of ways - with another variable  
string to replace on, or with one missing, etc. What I would really like  
to do is replace the string in-place of the one that I am searching for.  
Is there a built in ruby function to do this or can someone gets me some  
pointers about writing one myself? Thanks,

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [August 31, 2009, 6:42pm UTC](https://www.ruby-forum.com/t/string-replace/174707/2 "2009-08-31T18:42:50Z")

</div>

I got it: @freeform = @freeform.sub(/[W\_FAX]/, “555-555-5555”). I  
guess that’s what I get for not reading a little further in my Ruby  
book.

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [August 31, 2009, 6:53pm UTC](https://www.ruby-forum.com/t/string-replace/174707/3 "2009-08-31T18:53:50Z")

</div>

On Tue, 1 Sep 2009 01:42:52 +0900  
Shandy N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @freeform = @freeform.sub(/[W\_FAX]/, “555-555-5555”)

Also consider:

> @freeform.sub!(/[W\_FAX]/, “555-555-5555”)

With “sub!”, you can change the variable in-place.

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [August 31, 2009, 6:55pm UTC](https://www.ruby-forum.com/t/string-replace/174707/4 "2009-08-31T18:55:00Z")

</div>

2009/8/31 Shandy N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> I got it: @freeform = @freeform.sub(/[W\_FAX]/, “555-555-5555”). I  
> guess that’s what I get for not reading a little further in my Ruby  
> book.

You might also want String#gsub – #sub only replaces the first match,  
#gsub  
(“global substitute”) replaces all matches.
