Idanis
Idanis

Reputation: 1988

Trimming slashes from string c#

I have a string which, for some reason, sometimes looks like this: string a = "\"C:\\Temp\\1.bat", and sometimes looks normal, like this: string a = "C:\\Temp\\1.bat"

How do I fin out if I need to trim the first two "\ from the string or not, and trim it if necessary? In the end, I wish to be left with: "C:\\Temp\\1.bat" anyway.

Upvotes: 2

Views: 3298

Answers (2)

JLRishe
JLRishe

Reputation: 101652

You can do this:

a = a.TrimStart('\"');

Incidentally, you don't want to remove the "\ from the beginning of the string. That's impossible. What you want to remove is the \".

Upvotes: 4

Parimal Raj
Parimal Raj

Reputation: 20565

Your string is actually "C:\Temp\1.bat , the \" is the escaped form of ", so you only need to remove the first character!

string a = "\"C:\\Temp\\1.bat" 
string b = s.SubString(1);

Upvotes: 1

Related Questions