关于斜杠(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   中道学友  阅读(4369)  评论(1编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2011-08-29 TermStore offline的问题
2010-08-29 Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.

导航

< 2012年8月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8

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

点击右上角即可分享
微信分享提示