prosseek
prosseek

Reputation: 190769

Replacing all the '\' chars to '/' with C#

How can I replace all the '\' chars in a string into '/' with C#? For example, I need to make @"c:/abc/def" from @"c:\abc\def".

Upvotes: 13

Views: 82208

Answers (7)

kmerkle
kmerkle

Reputation: 58

string first = @"c:/abc/def";
string sec = first.Replace("/","\\");

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038770

The Replace function seems suitable:

string input = @"c:\abc\def";
string result = input.Replace(@"\", "/");

And be careful with a common gotcha:

Due to string immutability in .NET this function doesn't modify the string instance you are invoking it on => it returns the result.

Upvotes: 37

msarchet
msarchet

Reputation: 15242

@"C:\abc\def\".Replace(@"\", @"/");

Upvotes: 0

string result = @"c:\asb\def".Replace(Path.DirectorySeparatorChar,Path.AltDirectorySeparatorChar);

Upvotes: 0

Khepri
Khepri

Reputation: 9627

var origString = origString.Replace(@"\", @"/");

Upvotes: 1

Ed Swangren
Ed Swangren

Reputation: 124642

var replaced = originalStr.Replace( "\\", "/" );

Upvotes: 2

Bora
Bora

Reputation: 291

You need to escape the \

mystring.Replace("\\", "/");

Upvotes: 2

Related Questions