@转义符

如果必须嵌入 C# 提供的转义符,则应使用正则字符串,如下面的示例所示:

string columns = "Column 1\tColumn 2\tColumn 3";
//Output: Column 1        Column 2        Column 3

string rows = "Row 1\r\nRow 2\r\nRow 3";
/* Output:
  Row 1
  Row 2
  Row 3
*/

string title = "\"The \u00C6olean Harp\", by Samuel Taylor Coleridge";
//Output: "The �0�4olean Harp", by Samuel Taylor Coleridge


如果字符串文本包含反斜杠字符(例如在文件路径中),为方便起见和提高可读性,应使用原义字符串由于原义字符串保留换行符作为字符串文本的一部分,因此可用于初始化多行字符串在原义字符串中嵌入引号时请使用双引号。 下面的示例演示原义字符串的一些常见用途:

string filePath = @"C:\Users\scoleridge\Documents\";
//Output: C:\Users\scoleridge\Documents\

string text = @"My pensive SARA ! thy soft cheek reclined
    Thus on mine arm, most soothing sweet it is
    To sit beside our Cot,...";
/* Output:
My pensive SARA ! thy soft cheek reclined
   Thus on mine arm, most soothing sweet it is
   To sit beside our Cot,... 
*/

string quote = @"Her name was ""Sara.""";
//Output: Her name was "Sara."


posted @ 2013-01-22 23:29  kyoui  阅读(812)  评论(0编辑  收藏  举报