سؤال

I have this:

a = "whut.\\nErgh"

What I want to achieve is:

"whut.\nErgh" #sub 2 backslashes with 1 backslash

I tried this:

a.gsub(/\\\\/) { '\\' }

but it still returns me two backslashes.

Could someone please explain what went wrong here?

هل كانت مفيدة؟

المحلول

There are not two backslashes in "whut.\\nErgh" but just one.

"\\" is just one backslash char, the first \ is used to escape the backslash in a string.

If you want to convert \\n to a newline char, then use:

"whut.\\nErgh".gsub(/\\n/, "\n")

نصائح أخرى

Try this :

"whut.\\nErgh".gsub(/\\n/, "")
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top