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…

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

path = “C:\Gaurang\ruby\demo.xls”

is not valid! you NEED
path = “C:\Gaurang\ruby\demo.xls”
or
path = ‘C:\Gaurang\ruby\demo.xls’

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

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

Hans M. 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.

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”

On Fri, Jul 22, 2011 at 12:05:54AM +0900, Gaurang S. 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("\", “\\”)

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:23:26AM +0900, nick s. wrote:

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

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

On Thu, Jul 21, 2011 at 10:05 AM, Gaurang S. [email protected]
wrote:

Hi all,

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

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