搜索换行符 C#.net

发布于 2025-01-02 20:57:41 字数 267 浏览 4 评论 0原文

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

 theJ = line.IndexOf(Environment.NewLine);

或者

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

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

How do i search a string for a newline character? Both of the below seem to be returning -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\");"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

卷耳 2025-01-09 20:57:41
"yo\n" // output as "yo" + newline
"yo\n".IndexOf('\n') // returns 2
"yo\\n" // output as "yo\n"
"yo\\n".IndexOf('\n') // returns -1

您确定要搜索 yo\n 而不是 yo\\n 吗?

编辑

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

printf("yo\n");

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

printf("yo
");

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

line.IndexOf("\\n")

例如:

"\tprintf(\"yo\\n\");" // output as "  printf("yo\n");"
"\tprintf(\"yo\\n\");".IndexOf("\\n") // returns 11
"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\n and not 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 escaped newline 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:

line.IndexOf("\\n")

For example:

"\tprintf(\"yo\\n\");" // output as "  printf("yo\n");"
"\tprintf(\"yo\\n\");".IndexOf("\\n") // returns 11
倒数 2025-01-09 20:57:41

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

如果您使用的是 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
}

Looks like your line does not contain a newline.

If you are using File.ReadAllLines or string.Split on newline, then each line in the returned array will not contain the newline. If you are using StreamReader or one of the classes inheriting from it, the ReadLine method will return the string without the newline.

string lotsOfLines = @"one
two
three";

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

foreach(string line in lines)
{
  Console.WriteLine(line.IndexOf('\n'); // prints -1 three times
}
离去的眼神 2025-01-09 20:57:41

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

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

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

-1 simply means that no enter was found.

半﹌身腐败 2025-01-09 20:57:41

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

Environment.NewLine 返回:

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

另外:

  • 如果您想搜索 \n 字符(Unix 上的新行),请使用 \n
  • 如果您想搜索 \r\n 字符(Windows 上的新行),请使用 \r\n
  • 如果您的搜索取决于当前平台,请使用 Environment.NewLine

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

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

Environment.NewLine returns:

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

Also:

  • If you want to search for the \n char (new line on Unix), use \n
  • If you want to search for the \r\n chars (new line on Windows), use \r\n
  • If your search depend on the current platform, use Environment.NewLine

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

谁人与我共长歌 2025-01-09 20:57:41

当我上大学时,我做了一个 WebForms 应用程序来订购参考文献。

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

        //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;

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

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;
梦萦几度 2025-01-09 20:57:41

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文