# String substitution?

**URL:** <https://www.ruby-forum.com/t/string-substitution/93129>\
**Category:** Ruby\
**Created:** [April 27, 2007, 11:44pm UTC](https://www.ruby-forum.com/t/string-substitution/93129 "2007-04-27T23:44:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![azazel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@azazel](https://www.ruby-forum.com/u/azazel)\
**Post date:** [April 27, 2007, 11:44pm UTC](https://www.ruby-forum.com/t/string-substitution/93129/1 "2007-04-27T23:44:19Z")

</div>

Hello,

Im working on a small rails app that uses a text editor component  
(FCKEdit). In certain cases, the editor will add characters to my string  
that I need to remove before I show the output.

For example, it will add ‘’ in front of any ‘"’ or ‘’ in my document.

I was checking out String.replace and String.gsub, but they both seem to  
replace any element that matches the input string.

“hello”.gsub(/[aeiou]/, '_’) #=\> "h_ll\*"

In my particular case, I need it to match the whole string for  
substitution.

I worked out this code in order to substitute ‘"’ for ‘"’ and ‘\’ for  
‘’:

\<%= text.split(’"’).join(’"’).split("\\").join("\") %\>

Im sure there’s gotta be some other way to make it work without breaking  
it into arrays and rejoining.

Any help will be appreciated.  
az

---

<div class="post-metadata">

**Author:** ![azazel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@azazel](https://www.ruby-forum.com/u/azazel)\
**Post date:** [April 27, 2007, 11:56pm UTC](https://www.ruby-forum.com/t/string-substitution/93129/2 "2007-04-27T23:56:27Z")

</div>

On 4/27/07, Roberto R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> “hello”.gsub(/[aeiou]/, '_') #=\> "h_ll\*"
> 
> In my particular case, I need it to match the whole string for  
> substitution.
> 
> I worked out this code in order to substitute ‘"’ for ‘"’ and ‘\’ for  
> '':
> 
> \<%= text.split(‘"’).join(‘"’).split(“\\”).join(“\”) %\>  
> First of all there is nothing wrong with this(1), I like it a lot,  
> however as you have asked;)

text.gsub(‘"’,‘"’).gsub(“\\”,“\”)

might indeed be considered a little bit more concise.

Funny how this pattern just matches my previous post in the string  
replacement thread.

HTH  
Robert  
(1) With the exception of a trailing " or \"

---

<div class="post-metadata">

**Author:** ![azazel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@azazel](https://www.ruby-forum.com/u/azazel)\
**Post date:** [April 28, 2007, 12:19am UTC](https://www.ruby-forum.com/t/string-substitution/93129/3 "2007-04-28T00:19:53Z")

</div>

Robert D. wrote:

> On 4/27/07, Roberto R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > “hello”.gsub(/[aeiou]/, '_') #=\> "h_ll\*"
> > 
> > In my particular case, I need it to match the whole string for  
> > substitution.
> > 
> > I worked out this code in order to substitute ‘"’ for ‘"’ and ‘\’ for  
> > '':
> > 
> > \<%= text.split(‘"’).join(‘"’).split(“\\”).join(“\”) %\>  
> > First of all there is nothing wrong with this(1), I like it a lot,  
> > however as you have asked;)
> 
> text.gsub(‘"’,‘"’).gsub(“\\”,“\”)
> 
> might indeed be considered a little bit more concise.
> 
> Funny how this pattern just matches my previous post in the string  
> replacement thread.
> 
> HTH  
> Robert  
> (1) With the exception of a trailing " or \"

Indeed, I was trying to get a more concise alternative. Yours is  
perfect.  
Thanks
