How to replace "\" from string

Hi all,

I have string something like
String path = "C:\Gaurang\ruby\demo.xls"

would someone let me know how to replace "\" with "\\"

I tried something like below

path.gsub("\\", "\\\\")

but it's not working..

···

--
Posted via http://www.ruby-forum.com/.

This is a topic which comes up over and over again. I am sure you'll
find it in the history of this forum.

Kind regards

robert

···

--
Posted via http://www.ruby-forum.com/.

path = "C:\Gaurang\ruby\demo.xls"

is not valid! you NEED
path = "C:\\Gaurang\\ruby\\demo.xls"
or
path = 'C:\Gaurang\ruby\demo.xls'

···

--
Posted via http://www.ruby-forum.com/.

I believe you need to add delimiters, it should look like
Path.gsub(/"\\", "\\\\"/)

···

--
Posted via http://www.ruby-forum.com/.

no nick,s. your code is very wrong:
1. var is named wrong
2. gsub wants in this form 2 Strings not one Regex
3. it does not work because there is no "\\" inside path

···

--
Posted via http://www.ruby-forum.com/.

ya
what work is
path.gsub("\\","\\\\")
but this also not work BECAUSE your main string path has no "\\" inside
so gsub does not replace something and you can not replace the \ inside
path = "C:\Gaurang\ruby\demo.xls"

because its interpreted as \G \r and \d

if you does not want to write
path = "C:\\Gaurang\\ruby\\demo.xls"

you chould write inside ' ' (the char above #)
path = 'C:\Gaurang\ruby\demo.xls'

you can do nothing with "C:\Gaurang\ruby\demo.xls"

···

--
Posted via http://www.ruby-forum.com/.

Are you _assigning_ the return value of the above call to another object
(or back to path)?

HINT: gsub doesn't alter the String but instead _returns_ one with the
altered value: but gsub! (not the !) alters the existing string.

···

On Fri, Jul 22, 2011 at 12:05:54AM +0900, Gaurang Shah wrote:

Hi all,

I have string something like
String path = "C:\Gaurang\ruby\demo.xls"

would someone let me know how to replace "\" with "\\"

I tried something like below

path.gsub("\\", "\\\\")

--
Darryl L. Pierce <mcpierce@gmail.com>
http://mcpierce.multiply.com/
"What do you care what people think, Mr. Feynman?"

At this point, print the string and you will see easily what the problem is.

···

On Thu, Jul 21, 2011 at 10:05 AM, Gaurang Shah <shahgomji@gmail.com> wrote:

Hi all,

I have string something like
String path = "C:\Gaurang\ruby\demo.xls"

Hans Mackowiak wrote in post #1012179:

no nick,s. your code is very wrong:
1. var is named wrong
2. gsub wants in this form 2 Strings not one Regex
3. it does not work because there is no "\\" inside path

I have used "\\" instead of "\" coz "\" is a escape character.

I can't write something line path.gsub("\","\\")

it would be compile time error.

···

--
Posted via http://www.ruby-forum.com/\.

No, you don't. It's two separate arguments.

···

On Fri, Jul 22, 2011 at 12:23:26AM +0900, nick s. wrote:

I believe you need to add delimiters, it should look like
Path.gsub(/"\\", "\\\\"/)

--
Darryl L. Pierce <mcpierce@gmail.com>
http://mcpierce.multiply.com/
"What do you care what people think, Mr. Feynman?"