我如何找到" \ n"在一个字符串?

时间:2017-04-16 21:32:13

标签: c# string unity3d

我试图找到一个" \ 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);
    }
 }

3 个答案:

答案 0 :(得分:0)

\n不是两个追逐者。它是值&#34;换行符&#34;的两个字符表示。 (请参阅任何ASCII图表上的10的值)。如果要查找字符,则需要在单个字符'\n'

中使用这两个符号

答案 1 :(得分:0)

\ n是控制字符,请尝试作为条件

text [i] ==&#39; \ n&#39;

和init i = 0,而不是i = 1

答案 2 :(得分:0)

正确地说&#39; \ n&#39;是一个字符,表示换行符。这意味着,斜杠是转义键,例如用于表示特殊的空白字符(As \ n是1)。如果要搜索它,则必须将其作为单个字符进行搜索:

var text = "hello\nworld";
for (var i = 0; i < text.Length; i++)
    if(text[i] == '\n')
        Debug.Log($"break at: {i}");
相关问题