搜索换行符 C#.net

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9134550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 06:07:13  来源:igfitidea点击:

Search for a newline Character C#.net

c#.net

提问by BigBug

How do i search a string for a newline character? Both of the below seem to be returning -1....!

如何在字符串中搜索换行符?以下两个似乎都返回-1 ....!

 theJ = line.IndexOf(Environment.NewLine);

OR

或者

 theJ = line.IndexOf('\n');

The string it's searching is "yo\n" the string i'm parsing contains this "printf("yo\n");" the string i see contained during the comparison is this: "\tprintf(\"yo\n\");"

它正在搜索的字符串是 "yo\n" 我正在解析的字符串包含这个 "printf("yo\n");" 我在比较过程中看到的字符串是这样的:"\tprintf(\"yo\n\");"

采纳答案by StriplingWarrior

"yo\n" // output as "yo" + newline
"yo\n".IndexOf('\n') // returns 2
"yo\n" // output as "yo\n"
"yo\n".IndexOf('\n') // returns -1

Are you sure you're searching yo\nand not yo\\n?

你确定你在搜索yo\n而不是yo\\n

Edit

编辑

Based on your update, I can see that I guessed correctly. If your string says:

根据您的更新,我可以看到我猜对了。如果你的字符串说:

printf("yo\n");

... then this does not contain a newline character. If it did, it would look like this:

... 那么这不包含换行符。如果是这样,它看起来像这样:

printf("yo
");

What it actually has is an escapednewline character, or in other words, a backslash character followed by an 'n'. That's why the string you're seeing when you debug is "\tprintf(\"yo\\n\");". If you want to find this character combination, you can use:

它实际拥有的是一个转义的换行符,或者换句话说,一个反斜杠字符后跟一个“n”。这就是为什么您在调试时看到的字符串是"\tprintf(\"yo\\n\");". 如果要查找此字符组合,可以使用:

line.IndexOf("\n")

For example:

例如:

"\tprintf(\"yo\n\");" // output as "  printf("yo\n");"
"\tprintf(\"yo\n\");".IndexOf("\n") // returns 11

回答by Oded

Looks like your line does not contain a newline.

看起来您的行不包含换行符。

If you are using File.ReadAllLinesor string.Spliton newline, then each line in the returned array will not contain the newline. If you are using StreamReaderor one of the classes inheriting from it, the ReadLinemethod will return the string withoutthe newline.

如果您使用File.ReadAllLinesstring.Split换行符,则返回数组中的每一行都将不包含换行符。如果您正在使用StreamReader或继承自它的类之一,该ReadLine方法将返回不带换行符的字符串。

string lotsOfLines = @"one
two
three";

string[] lines = lotsOfLines.Split('\n');

foreach(string line in lines)
{
  Console.WriteLine(line.IndexOf('\n'); // prints -1 three times
}

回答by Carra

That should work although in Windowsyou'll have to search for '\r\n'.

尽管在Windows 中您必须搜索“ \r\n”,但这应该可行。

-1 simply means that no enter was found.

-1 只是意味着没有找到输入。

回答by Josh

The Environment.NewLine is not the same as \n. It is a CRLF (\r\n). However, I did try with the \n using IndexOf and my test did find the value. Are you sure what you're searching for is a \n rather than a \r? View your text in hexadecimal format and see what the hex value is.

Environment.NewLine 与 \n 不同。它是一个 CRLF (\r\n)。但是,我确实尝试使用 IndexOf 使用 \n 并且我的测试确实找到了该值。您确定要搜索的是 \n 而不是 \r 吗?查看十六进制格式的文本并查看十六进制值是什么。

回答by ken2k

It depends what you are trying to do. Both may no be identical on some platforms.

这取决于你想要做什么。在某些平台上两者可能不相同。

Environment.NewLinereturns:

Environment.NewLine返回:

A string containing "\r\n" for non-Unix platforms, or a string containing "\n" for Unix platforms.

包含“\r\n”的字符串用于非 Unix 平台,或包含“\n”的字符串用于 Unix 平台。

Also:

还:

  • If you want to search for the \nchar (new line on Unix), use \n
  • If you want to search for the \r\nchars (new line on Windows), use \r\n
  • If your search depend on the current platform, use Environment.NewLine
  • 如果要搜索\n字符(Unix 上的新行),请使用\n
  • 如果要搜索\r\n字符(Windows 上的新行),请使用\r\n
  • 如果您的搜索依赖于当前平台,请使用 Environment.NewLine

If it returns -1 in both cases you mentioned, then you don't have a new line in your string.

如果它在您提到的两种情况下都返回 -1,那么您的字符串中就没有新行。

回答by Anderson Rissardi

When I was in college and I did a WebForms aplication to order referencies.

当我在大学时,我做了一个 WebForms 应用程序来订购参考资料。

And the line break/carriage return it was what I used to break a referense.

换行符/回车符是我用来打破引用的。

        //Text from TextBox
        var text = textBox1.Text;

        //Create an array with the text between the carriage returns
        var references = text.Split(new string[] { "\r\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);

        //Simple OrderBy(Alphabetical)
        var ordered = references.ToList<string>().OrderBy(ff => ff);

        //Return the entry text ordered alphabetical em with a carriage return between every result
        var valueToReturn = String.Join(Environment.NewLine, ordered);
        textBox1.Text = valueToReturn;