user488963
user488963

Reputation: 119

How to write escape character to string?

How to write escape character "\" to string?

Upvotes: 5

Views: 56424

Answers (6)

Powerlord
Powerlord

Reputation: 88786

In a normal string, you use \\

If this is in a regular expression, it needs to be \\\\

The reason RegEx needs four is because both the String parser and RegEx engine support escapes. Therefore, \\\\ is parsed to \\ by the String parser then to a literal \ by the RegEx parser.

Upvotes: 13

codaddict
codaddict

Reputation: 454940

You can write:

String newstr = "\\"; 

\ is a special character within a string used for escaping.

"\" does now work because it is escaping the second ".

To get a literal \ you need to escape it using \.

Upvotes: 0

Patrick
Patrick

Reputation: 7712

string = @"c:\inetpub\wwwroot\"

is the same as

 string = "c:\\inetpub\\wwwroot\\"

in some cases... i'm not 100% sure but i think it might be language dependant... i KNOW "@" works in C#

Upvotes: 0

pyCoder
pyCoder

Reputation: 501

Use double \ in string

String example = "C:\\Program Files";

Upvotes: 0

Starkey
Starkey

Reputation: 9781

Escape the escape character: "\\"

Upvotes: 4

Bryan Denny
Bryan Denny

Reputation: 27596

You escape certain characters by adding "\" in front of the character.

So \\ will work for you.

Upvotes: 0

Related Questions