搜索换行符 C#.net
如何在字符串中搜索换行符?下面的两个似乎都返回-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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您确定要搜索
yo\n
而不是yo\\n
吗?编辑
根据您的更新,我可以看到我猜对了。如果您的字符串显示:
... 那么它不包含换行符。如果是的话,它看起来像这样:
它实际上有一个转义换行符,或者换句话说,一个反斜杠字符后跟一个“n”。这就是为什么您在调试时看到的字符串是
"\tprintf(\"yo\\n\");"
。如果你想找到这个字符组合,可以使用:例如:
Are you sure you're searching
yo\n
and notyo\\n
?Edit
Based on your update, I can see that I guessed correctly. If your string says:
... then this does not contain a newline character. If it did, it would look like this:
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:For example:
看起来您的行不包含换行符。
如果您使用的是
File.ReadAllLines
或string.Split
上换行符,那么返回数组中的每一行都不会包含换行符。如果您使用的是 StreamReader 或从其继承的类之一,则ReadLine
方法将返回不带换行符的字符串。Looks like your line does not contain a newline.
If you are using
File.ReadAllLines
orstring.Split
on newline, then each line in the returned array will not contain the newline. If you are usingStreamReader
or one of the classes inheriting from it, theReadLine
method will return the string without the newline.虽然在 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.
这取决于你想做什么。在某些平台上两者可能不相同。
Environment.NewLine
返回:另外:
\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:Also:
\n
char (new line on Unix), use\n
\r\n
chars (new line on Windows), use\r\n
Environment.NewLine
If it returns -1 in both cases you mentioned, then you don't have a new line in your string.
当我上大学时,我做了一个 WebForms 应用程序来订购参考文献。
换行符/回车符是我用来破坏引用的。
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.
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.