• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 529
  • Last Modified:

Replace double backslash in a string

Hi

I have a string which (fro example) looks like this ...

C:\\temp\\MediaCoverage\\Waiting

I want to replace all '\\' with '\'

I try this ...

WaitingFolder = WaitingFolder.Replace("\\\\", "\\");
WaitingFolder = WaitingFolder.Replace(@"\\", @"\");

but neither work

Any ideas?

thanks
0
hmcgeehan
Asked:
hmcgeehan
  • 4
  • 4
1 Solution
 
Raymond-HolmboeCommented:
Hi

Did you try Replace("\\", "whatever") ?
0
 
Ryan ChongCommented:
no need to replace \\ to \, \\ is the escape to show character \ in C#.
0
 
hmcgeehanAuthor Commented:
Raymond-Holmboe:

That gives me ....
C:whatevertempwhateverMediaCoveragewhateverWaiting

So it does work for that ....

But if I do

string newWaitingFolder = WaitingFolder.Replace("\\", @"\");

it doesn't change the original string.

And if I do
string newWaitingFolder = WaitingFolder.Replace("\\", "\");
it won't compile
Error      1      Newline in constant

thanks
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
hmcgeehanAuthor Commented:
ryancys

If I don't do the replace then when I try to do this ....

if (System.IO.File.Exists(fileToUpload))

it says 'File not found'

If I give it a string with one \ instead of \\ it will work fine.
0
 
Ryan ChongCommented:
String a = "\\";

and

String b = @"\";


both contain same value....
0
 
Ryan ChongCommented:
is fileToUpload returns a valid file name?

can you show the scripts to get fileToUpload ?
0
 
hmcgeehanAuthor Commented:
String fileToUpload = @WaitingFolder + hfDocumentName.Value;

And WaitingFolder = "C:\\temp\\MediaCoverage\\Waiting"
0
 
Ryan ChongCommented:
hfDocumentName.Value contains "\\" at the start?

if it's not, you may try this instead:

...

String WaitingFolder = @"C:\temp\MediaCoverage\Waiting\";

then:

String fileToUpload = WaitingFolder + hfDocumentName.Value;

...
0
 
hmcgeehanAuthor Commented:
hfDocumentName.Value contains "\\" at the start?

Doh it didn't
Sorry guys my bad!
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now