關於斜杠(slash)和反斜線(back slash)的小知識點

來源:互聯網
上載者:User

這兩個總容易記混, 其實英文版的更好記一些, 往前倒的叫斜杠, 往後倒的叫反斜線. 呵呵.

 

在WinDBG中, 撈到一個DirectorySearcher的Filter成員的值如下:

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查詢使用者的時候, 發現這裡的斜杠再過編譯時間遇到了點問題.

嘗試過在字串前面添加一個@符號, 可是沒有用, 還是過不了編譯.

把所有的反斜線都變成雙反斜線, 可以發現, 這個值是正確的了. 如下:

.

驗證

 

這裡用到了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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.