# Question about strings

**URL:** <https://www.ruby-forum.com/t/question-about-strings/73731>\
**Category:** Ruby\
**Created:** [October 26, 2006, 6:25pm UTC](https://www.ruby-forum.com/t/question-about-strings/73731 "2006-10-26T18:25:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aladrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f8ce5/32.png) [@aladrin](https://www.ruby-forum.com/u/aladrin)\
**Post date:** [October 26, 2006, 6:25pm UTC](https://www.ruby-forum.com/t/question-about-strings/73731/1 "2006-10-26T18:25:04Z")

</div>

Okay, given the following string:

“This is Will’s string!”

what I’m trying to do is replace the single-tick “’” character with a  
backslash/single-tick “’”, so I can use use that string in a sql query.

To do this in Perl, you’d do the following:

myString =~ s/’//g;

…which doesn’t work in Ruby.

I also tried using the gsub function, with “\’” passed in as the  
second parameter, and it just started outputting some sort sort of  
goofiness with characters replaced by sub-elements of the string for  
which I was calling the function on and the like.

I scoured Google for awhile but didn’t find anything. Any ideas?

---

<div class="post-metadata">

**Author:** ![aladrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f8ce5/32.png) [@aladrin](https://www.ruby-forum.com/u/aladrin)\
**Post date:** [October 26, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/question-about-strings/73731/2 "2006-10-26T18:58:22Z")

</div>

- William C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) [2006-10-26 10:25]:

> I also tried using the gsub function, with “\'” passed in as the  
> second parameter, and it just started outputting some sort sort of  
> goofiness with characters replaced by sub-elements of the string for  
> which I was calling the function on and the like.

Your replacement string ends up as ', which is the equivalent of Perl’s  
$’ (the substring from the match to the end).

The best solution I could find was to match the space before the  
quotation mark.

> > “I am Will’s string!”.gsub(/(?=‘)/,’\')  
> > =\> “I am Will\'s string!”

---

<div class="post-metadata">

**Author:** ![aladrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f8ce5/32.png) [@aladrin](https://www.ruby-forum.com/u/aladrin)\
**Post date:** [October 26, 2006, 7:06pm UTC](https://www.ruby-forum.com/t/question-about-strings/73731/3 "2006-10-26T19:06:42Z")

</div>

Grant Hollingworth wrote:

> The best solution I could find was to match the space before the  
> quotation mark.
> 
> > > “I am Will’s string!”.gsub(/(?=’)/,’\’)  
> > > =\> “I am Will\'s string!”

Using a block for the substitution appears to work:

> > “This is Will’s string!”.gsub(/(’)/) {|m| ‘\’ + m}  
> > =\> “This is Will\'s string!”

---

<div class="post-metadata">

**Author:** ![aladrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f8ce5/32.png) [@aladrin](https://www.ruby-forum.com/u/aladrin)\
**Post date:** [October 26, 2006, 7:40pm UTC](https://www.ruby-forum.com/t/question-about-strings/73731/4 "2006-10-26T19:40:30Z")

</div>

On Fri, 27 Oct 2006, William C. wrote:

> …which doesn’t work in Ruby.
> 
> I also tried using the gsub function, with “\’” passed in as the  
> second parameter, and it just started outputting some sort sort of  
> goofiness with characters replaced by sub-elements of the string for  
> which I was calling the function on and the like.
> 
> I scoured Google for awhile but didn’t find anything. Any ideas?

irb(main):001:0\> “This is Will’s string!”.gsub(/’/){ %q(’) }  
=\> “This is Will\'s string!”

-a

---

<div class="post-metadata">

**Author:** ![aladrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f8ce5/32.png) [@aladrin](https://www.ruby-forum.com/u/aladrin)\
**Post date:** [October 26, 2006, 7:26pm UTC](https://www.ruby-forum.com/t/question-about-strings/73731/5 "2006-10-26T19:26:27Z")

</div>

William C. wrote:

/ …

> I scoured Google for awhile but didn’t find anything. Any ideas?

#!/usr/bin/ruby -w

a = “This is Will’s string!”

b = a.gsub(%r{’},"\\’")

puts a,b

Output:

This is Will’s string!  
This is Will’s string!

---

<div class="post-metadata">

**Author:** ![aladrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f8ce5/32.png) [@aladrin](https://www.ruby-forum.com/u/aladrin)\
**Post date:** [October 26, 2006, 7:56pm UTC](https://www.ruby-forum.com/t/question-about-strings/73731/6 "2006-10-26T19:56:32Z")

</div>

William C. wrote:

> Okay, given the following string:
> 
> “This is Will’s string!”
> 
> what I’m trying to do is replace the single-tick “’” character with a  
> backslash/single-tick “’”, so I can use use that string in a sql query.

Can’t you use the SQL-engine escape-method, if it exists?

Stefan

---

<div class="post-metadata">

**Author:** ![aladrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f8ce5/32.png) [@aladrin](https://www.ruby-forum.com/u/aladrin)\
**Post date:** [October 26, 2006, 9:16pm UTC](https://www.ruby-forum.com/t/question-about-strings/73731/7 "2006-10-26T21:16:40Z")

</div>

William C. wrote:

> …which doesn’t work in Ruby.
> 
> I also tried using the gsub function, with “\’” passed in as the  
> second parameter, and it just started outputting some sort sort of  
> goofiness with characters replaced by sub-elements of the string for  
> which I was calling the function on and the like.
> 
> I scoured Google for awhile but didn’t find anything. Any ideas?

This comes up from time to time. The crucial bit is to understand the  
levels of quoting / interpretation involved. Here is a general escaping  
piece:

irb(main):017:0\> puts “This is Will’s string!”.gsub(/’/, ‘\\\&’)  
This is Will’s string!  
=\> nil

Explanation:

1. level is the string interpretation. You need to escape a backslash  
to have him literally in a string:

irb(main):019:0\> puts ‘\’  
  
=\> nil

1. the RX engine uses backslash as escape character as well, for example  
to reference groups with \1

So you need four (4) backslashes to make the RX engine insert a single  
backslash:

irb(main):020:0\> puts “This is Will’s string!”.gsub(/’/, ‘\\’)  
This is Will\s string!  
=\> nil

1. the RX engine uses & as a placeholder for the match:

irb(main):022:0\> puts “This is Will’s string!”.gsub(/’/, ‘\<\&\>’)  
This is Will\<’\>s string!  
=\> nil

Combined makes 3 \* 2 = 6 backslashes.

Bonus reply: although the block form is easier on the eye it is actually  
less performant because the block has to be evaluated for every match.  
This is rather intended for replacements that are not static, i.e. where  
the replacement depends in a way on the match that cannot be expressed  
with the usual mechanisms in the replacement string. For example:

irb(main):023:0\> c=0; puts “This is Will’s string!”.gsub(/i/) { c+= 1 }  
Th1s 2s W3ll’s str4ng!  
=\> nil

Kind regards

```
robert
```
