关于斜杠(slash)和反斜杠(back slash)的小知识点

这两个总容易记混, 其实英文版的更好记一些, 往前倒的叫斜杠, 往后倒的叫反斜杠. 呵呵.

 

在WinDBG中, 捞到一个DirectorySearcherFilter成员的值如下:

0:014> !do rdx

Unable to enumerate managed locals, HRESULT 0x80004001

Unable to enumerate managed locals, HRESULT 0x80004001

Name: System.String

MethodTable: 000007fef91a7c18

EEClass: 000007fef8dae530

Size: 414(0x19e) bytes

GC Generation: 0

(C:\Windows\assembly\GAC_64\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll)

String: (&(objectSID=\01\05\00\00\00\00\00\05\15\00\00\00\EE\94\83\48\54\46\C0\31\27\DB\8F\4F\C4\73\04\00))

Fields:

MT Field Offset Type VT Attr Value Name

000007fef91aee88 4000096 8 System.Int32 1 instance 195 m_arrayLength

000007fef91aee88 4000097 c System.Int32 1 instance 99 m_stringLength

000007fef91a9660 4000098 10 System.Char 1 instance 28 m_firstChar

000007fef91a7c18 4000099 20 System.String 0 shared static Empty

>> Domain:Value 0000000000131320:00000000ff490370 0000000002709600:00000000ff490370 <<

000007fef91a9510 400009a 28 System.Char[] 0 shared static WhitespaceChars

>> Domain:Value 0000000000131320:00000000ff490ac0 0000000002709600:000000019f4955f0 <<

 

自己写了段C#代码使用这个值向AD查询用户的时候, 发现这里的斜杠再过编译时遇到了点问题.

尝试过在字符串前面添加一个@符号, 可是没有用, 还是过不了编译.

把所有的反斜杠都变成双反斜杠, 可以发现, 这个值是正确的了. 如下:

image.

验证

image

 

这里用到了C#中的转义字符的一些知识

C# defines the following character escape sequences:

  • \' - single quote, needed for character literals
  • \" - double quote, needed for string literals
  • \\ - backslash
  • \0 - Unicode character 0
  • \a - Alert (character 7)
  • \b - Backspace (character 8)
  • \f - Form feed (character 12)
  • \n - New line (character 10)
  • \r - Carriage return (character 13)
  • \t - Horizontal tab (character 9)
  • \v - Vertical quote (character 11)
  • \uxxxx - Unicode escape sequence for character with hex value xxxx
  • \xn[n][n][n] - Unicode escape sequence for character with hex value nnnn (variable length version of \uxxxx)
  • \Uxxxxxxxx - Unicode escape sequence for character with hex value xxxxxxxx (for generating surrogates)

Of these, \a, \f, \v, \x and \U are rarely used in my experience.

[Author: Jon Skeet]

 

关于@符号

这里以@为前缀的字符串叫做verbatim string. 举例:

这个路径的格式被认为是丑陋且尴尬的

string path = "C:\\Program Files\\Microsoft Visual Studio 10.0\\";

一个更方便的版本是使用verbatim string

string path = @"C:\Program Files\Microsoft Visual Studio 10.0\";

Verbatim string(@"...")将其内容认为为纯内容, 不需对任何字符进行转义. 然而, 有一个字符即使在verbatim string也须进行转义, 那就是内嵌的双引号("), 它必须被转义为(""). 举例:

string xml = @"<?xml version=""1.0""?>
<Data>
...
<Data>";

Reference

==================

What character escape sequences are available?

http://blogs.msdn.com/b/csharpfaq/archive/2004/03/12/what-character-escape-sequences-are-available.aspx

Escape Sequences

http://msdn.microsoft.com/en-us/library/h21280bw.aspx

Escaping in C#: characters, strings, string formats, keywords, identifiers

http://www.codeproject.com/Articles/371232/Escaping-in-Csharp-characters-strings-string-forma

posted on 2012-08-29 10:51  中道学友  阅读(3915)  评论(1编辑  收藏  举报

导航

技术追求准确,态度积极向上