.NET 字符串 IndexOf 意外结果
字符串变量 str 在其内部某处包含以下内容: se\">
我试图使用以下方法查找它的开头:
str.IndexOf("se\\\">")
返回 -1
为什么它找不到子字符串?
注意:由于编辑片段显示 5x \ 一段时间,原来的连续 3 个。
A string variable str contains the following somewhere inside it: se\">
I'm trying to find the beginning of it using:
str.IndexOf("se\\\">")
which returns -1
Why isn't it finding the substring?
Note: due to editing the snippet showed 5x \ for a while, the original had 3 in a row.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您正在寻找 se\">
那么
就不太容易出错。请注意双“”和单 \
编辑,在注释之后:看起来字符串可能包含转义本身,其中se\"> 中的情况 \" 是转义引号,因此文字文本只是 se"> ,字符串为使用的是
Indexof("se\">")
If you are looking for se\">
then
is less error-prone. Note the double "" and single \
Edit, after the comment: it seems like the string may contain ecaping itself, in which case in se\"> the \" was an escaped quote, so the literal text is simply se"> and the string to use is
Indexof("se\">")
您的代码实际上正在搜索
'se\\">'
。在搜索包含反斜杠的字符串时,我通常发现使用逐字字符串更容易:在这种情况下,您在搜索中还会有一个引号字符串,所以仍然有一些转义,但我个人认为它更容易阅读
更新:我的答案是基于在
IndexOf
根据当前版本,我认为str
根本不包含预期的字符序列更新 2:
根据对此答案的评论,对于字符串中“\”字符的作用似乎有些混乱。 当您在 Visual Studio 调试器中检查字符串时,它将显示带有转义字符。
因此,如果您有一个文本框并在其中键入“c:\”,则在调试器中检查 Text 属性将显示“c:\\”。 添加额外的反斜杠是为了转义目的。 实际的字符串内容仍然是“c:\”(可以通过检查字符串的
Length
属性来验证;它将是3,而不是4)。如果我们采用以下字符串(取自下面的评论)
...
\"
序列只是转义引号; 反斜杠不是字符串内容的一部分。 因此,您实际上正在寻找'se">'
,而不是'se\">'
。 其中任何一个都可以工作:Your code is in fact searching for
'se\\">'
. When searching for strings including backslashes I usually find it easier to use verbatim strings:In this case you also have a quote in the search string, so there is still some escaping, but I personally find it easier to read.
Update: my answer was based on the edit that introduced extra slashes in the parameter to the
IndexOf
call. Based on current version, I would place my bet onstr
simply not containing the expected character sequence.Update 2:
Based on the comments on this answer, it seems to be some confusion regarding the role of the '\' character in the strings. When you inspect a string in the Visual Studio debugger, it will be displayed with escaping characters.
So, if you have a text box and type 'c:\' in it, inspecting the Text property in the debugger will show 'c:\\'. An extra backslash is added for escaping purposes. The actual string content is still 'c:\' (which can be verified by checking the
Length
property of the string; it will be 3, not 4).If we take the following string (taken from the comment below)
...the
\"
sequences are simply escaped quotation marks; the backslashes are not part of the string content. So, you are in fact looking for'se">'
, not'se\">'
. Either of these will work:这有效:
也许您没有正确转义两个字符串之一?
编辑您正在搜索的字符串中有一对额外的
\
。This works:
Maybe you're not correctly escaping one of the two strings?
EDIT there's an extra couple of
\
in the string you are searching for.也许 str 变量实际上并不包含反斜杠。
可能只是当您在调试时将鼠标悬停在变量上时,调试器工具提示将显示转义字符。
例如,如果在此分配之后放置断点,
工具提示将显示 123\"456 而不是 123"456。
但是,如果单击可视化图标,您将获得正确的字符串 123"456
Maybe the str variable does not actually contain the backslash.
It may be just that when you mouse over the variable while debugging, the debugger tooltip will show the escape character.
e.g. If you put a breakpoint after this assignment
the tooltip will show 123\"456 and not 123"456.
However if you click on the visualize icon, you will get the correct string 123"456
以下代码:
给出以下输出:
似乎对我有用......
Following code:
Gives following output:
Seems like working to me...
好像在VB中可以工作。
seems to work in VB.
字符串中的双引号需要像
""
一样指定 还可以考虑使用逐字字符串 - 所以一个例子是DoubleQuotes within a string need to be specified like
""
Also consider using verbatim strings - So an example would be