ruby remove backslash from string

旧城冷巷雨未停 提交于 2019-12-06 03:22:05

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 "3e265629c­7ff56a3a88­505062dd52­6bd\"".del­ete ?"

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]
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!