I am working in asp.net mvc. want to replace \
character to /
character. But it is not working.
Let
string path="D:\Qamar\Cartoons\Image.jpg";
path=path.Replace("\","/");
I get error in second line. Please help how to replace.
I am working in asp.net mvc. want to replace \
character to /
character. But it is not working.
Let
string path="D:\Qamar\Cartoons\Image.jpg";
path=path.Replace("\","/");
I get error in second line. Please help how to replace.
Try this:
string path="D:\Qamar\Cartoons\Image.jpg";
path=path.Replace("\\","/");
You need to escape the backslash in the first argument for it to be treated as...a backslash (i.e. "\\"
instead of "\"
).
You need to escape back-slashes. The easiest way is to prefix your string with @:
path=path.Replace(@"\","/");
Another method is to escape it with another backslash:
path=path.Replace("\\","/");
\
is a special escape character in string literals in c#.
You can precede the string with @
to make it verbatim or escape the \
with another \
:
path=path.Replace(@"\","/");
or
path=path.Replace("\\","/");
\
is escape character, so your code will not even compile use @
or \\
to make the code compile. then it will work
string path=@"D:\Qamar\Cartoons\Image.jpg";
path=path.Replace(@"\","/");
or
string path="D:\\Qamar\\Cartoons\\Image.jpg";
path=path.Replace("\\","/");
but if you are working with Path or URI you can use the inbuilt C# methods to do it like below
System.Uri uri1 = new Uri(@"D:\Qamar\Cartoons\Image.jpg");
string whatYouWant = uri1.AbsolutePath; //Result is: "D:/Qamar/Cartoons/Image.jpg"