關於斜杠(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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM