奇怪的字符串结果

时间:2014-11-19 08:22:15

标签: c# string newline richtextbox

只是好奇它。我想知道为什么:

string str = @"//
    // Use a new char[] array of two characters (\r and \n) to break
    // lines from into separate strings. \n Use RemoveEmptyEntries
    // to make sure no empty strings get put in the string array. 
    //";

对richTextBox的结果文本是:

//
    // Use a new char[] array of two characters (\r and \n) to break
    // lines from into separate strings. \n Use RemoveEmptyEntries
    // to make sure no empty strings get put in the string array. 
    //

string str = @"//
    // Use a new char[] array of two characters (\r and \n) to break
    // lines from into separate strings."+" \n" + @" Use RemoveEmptyEntries
    // to make sure no empty strings get put in the string array. 
    //";

对richTextBox的结果文本是:

//
    // Use a new char[] array of two characters (\r and \n) to break
    // lines from into separate strings. 
 Use RemoveEmptyEntries
    // to make sure no empty strings get put in the string array. 
    //

2 个答案:

答案 0 :(得分:3)

在您的中心字符串之前没有文字@" \ n" 等价物是

string str = @"//
// Use a new char[] array of two characters (\r and \n) to break
// lines from into separate strings."+ @" \n" + @" Use RemoveEmptyEntries
// to make sure no empty strings get put in the string array. 
//";

\是普通字符串中的转义字符,但字符串文字完全按原样使用内容,除非是"被转义为""

答案 1 :(得分:1)

因为" \ n"之前没有@在你的第二个例子。 @(逐字字符串文字)之后的任何转义序列都将被忽略。在你的第一个例子中,它被忽略但不在第二个例子中。

看看这个: http://msdn.microsoft.com/en-us/library/aa691090(v=vs.71).aspx