How would I make this a regular string with no slash?
I have:
"3e265629c7ff56a3a88505062dd526bd\""
I want:
"3e265629c7ff56a3a88505062dd526bd"
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 \\
.
How is this?
s = "3e265629c7ff56a3a88505062dd526bd\""
s[/\w+/]
# => "3e265629c7ff56a3a88505062dd526bd"
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.
Just another way "3e265629c7ff56a3a88505062dd526bd\"".delete ?"
str = "3e265629c7ff56a3a88505062dd526bd\""
str.gsub(%r{\"}, '')
=> "3e265629c7ff56a3a88505062dd526bd"
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]
来源:https://stackoverflow.com/questions/18989513/ruby-remove-backslash-from-string