BC00
BC00

Reputation: 1639

ruby remove backslash from string

How would I make this a regular string with no slash?

I have:

 "3e265629c7ff56a3a88505062dd526bd\""

I want:

"3e265629c7ff56a3a88505062dd526bd"

Upvotes: 4

Views: 7493

Answers (6)

the Tin Man
the Tin Man

Reputation: 160631

Ruby's String#[] is your friend. Starting with:

foo = "3e...bd\""

These are alternate ways to get the value without the trailing embedded quote:

# delete it
foo[-1] = ''
foo['"'] = ''
foo[/"$/] = ''

Or:

# skip it
foo[0..-2]

Upvotes: 1

Mauricio Ulloa
Mauricio Ulloa

Reputation: 542

If you want to delete everything except letters and numbers you can use 'tr' function.

For example:

"3e265629c7ff56a3a88505062dd526bd\"".tr('^A-Za-z0-9','')

This function replace everything but letters and numbers with a string with no characters. Here is a reference of the function.

Hope it works for you.

Upvotes: 3

Raghu
Raghu

Reputation: 2563

Just another way "3e265629c­7ff56a3a88­505062dd52­6bd\"".del­ete ?"

Upvotes: 2

Arup Rakshit
Arup Rakshit

Reputation: 118299

How is this?

s = "3e265629c7ff56a3a88505062dd526bd\""
s[/\w+/]
# => "3e265629c7ff56a3a88505062dd526bd"

Upvotes: 5

Winfield
Winfield

Reputation: 19145

str = "3e265629c7ff56a3a88505062dd526bd\""
str.gsub(%r{\"}, '')
 => "3e265629c7ff56a3a88505062dd526bd"

Upvotes: 1

tadman
tadman

Reputation: 211740

What you have:

"3e265629c7ff56a3a88505062dd526bd\""

Is equivalent to:

'3e265629c7ff56a3a88505062dd526bd"'

So to remove that:

string.tr!('"', '')

Remember all special characters are prefixed with backslash. This includes not only things like newline \n or linefeed \r, but also quotation mark " or backslash itself \\.

Upvotes: 9

Related Questions