我尝试在字符串中找到“\ n”。我的代码在这里不起作用。什么是正确的方法?谢谢如何在字符串中找到“ n”?
string text = "hello\nworld";
for(int i=1; i<text.Length ;i++)
{
if
(
text[i-1]== '\\'
&&
text[i]== 'n'
)
{
Debug.Log("break at: "+i);
}
}
我尝试在字符串中找到“\ n”。我的代码在这里不起作用。什么是正确的方法?谢谢如何在字符串中找到“ n”?
string text = "hello\nworld";
for(int i=1; i<text.Length ;i++)
{
if
(
text[i-1]== '\\'
&&
text[i]== 'n'
)
{
Debug.Log("break at: "+i);
}
}
\n
是不是两个字符。它是“换行”值的两个字符表示(请参阅任何ASCII图表上的10的值)。如果你想找到的人物造型,你需要在单个字符使用两个符号像'\n'
感谢.. – cubecube
请参阅[C#语言规范](http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf)中的“9.4.4.4字符文字”。它表示'\ n'是[U + 000A LINE FEED(LF)]的转义序列(http://www.fileformat.info/info/unicode/char/000A/index.htm)。提到ASCII是误导性的。 .NET(以及Java,JavaScript,HTML,XML,... NCHAR,NVARCHAR,...)使用[Unicode]的UTF-16编码(http://www.unicode.org/charts/nameslist/index.html)。 –
正确地说'\ n'是一个字符,代表换行。这意味着,斜杠是转义键,例如用于表示特殊的空白字符(As \ n是一个)。如果你想寻找它,你因此必须寻找其作为单个字符:
var text = "hello\nworld";
for (var i = 0; i < text.Length; i++)
if(text[i] == '\n')
Debug.Log($"break at: {i}");
\ n是一个换行符,通常是由CRLF表示相信,所以你需要寻找CHAR(13) ,char(10),或者它可能只是char(10),认为\ r \ n是crlf – Charleh
@Charleh \ n只代表LF,\ r是CR –
是啊刚刚意识到:)更新了我的评论 – Charleh