Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use "\" in a string without making it an escape sequence - C#?

Tags:

c#

escaping

I'm sure this is something really basic that I don't know but how do I make it not recognize "\" as an escape sequence inside a string

I'm trying to type in a path and it thinks it is an escape sequence

like image 337
Jarred Sumner Avatar asked Nov 20 '09 02:11

Jarred Sumner


People also ask

How do I ignore an escape character in a string?

An escape sequence is a set of characters used in string literals that have a special meaning, such as a new line, a new page, or a tab. For example, the escape sequence \n represents a new line character. To ignore an escape sequence in your search, prepend a backslash character to the escape sequence.

How do you initiate a string without escaping each backslash?

You can use " \\ " instead of " \ " as well as '@' sign in the beginning.


1 Answers

You can use Verbatim String Literals:

//Initialize with a regular string literal.
string oldPath = "c:\\Program Files\\Microsoft Visual Studio 8.0";

// Initialize with a verbatim string literal.
string newPath = @"c:\Program Files\Microsoft Visual Studio 9.0";
                 ↑
like image 142
dtb Avatar answered Sep 18 '22 17:09

dtb