.NET 字符串 IndexOf 意外结果

发布于 2024-07-26 00:03:51 字数 207 浏览 2 评论 0原文

字符串变量 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 技术交流群。

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

发布评论

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

评论(7

嗼ふ静 2024-08-02 00:03:52

如果您正在寻找 se\">

那么

str.IndexOf(@"se\"">") 

就不太容易出错。请注意双“”和单 \

编辑,在注释之后:看起来字符串可能包含转义本身,其中se\"> 中的情况 \" 是转义引号,因此文字文本只是 se"> ,字符串为使用的是 Indexof("se\">")

If you are looking for se\">

then

str.IndexOf(@"se\"">") 

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\">")

甚是思念 2024-08-02 00:03:51

您的代码实际上正在搜索 'se\\">'。在搜索包含反斜杠的字符串时,我通常发现使用逐字字符串更容易:

str.IndexOf(@"se\"">")

在这种情况下,您在搜索中还会有一个引号字符串,所以仍然有一些转义,但我个人认为它更容易阅读

更新:我的答案是基于在 IndexOf 根据当前版本,我认为 str 根本不包含预期的字符序列

更新 2
根据对此答案的评论,对于字符串中“\”字符的作用似乎有些混乱。 当您在 Visual Studio 调试器中检查字符串时,它将显示带有转义字符。

因此,如果您有一个文本框并在其中键入“c:\”,则在调试器中检查 Text 属性将显示“c:\\”。 添加额外的反斜杠是为了转义目的。 实际的字符串内容仍然是“c:\”(可以通过检查字符串的Length属性来验证;它将是3,而不是4)。

如果我们采用以下字符串(取自下面的评论)

" '<他们
类=\"正确响应\">a
夜灯

><表
宽度=\"100%\">英格丽德')"

...\" 序列只是转义引号; 反斜杠不是字符串内容的一部分。 因此,您实际上正在寻找 'se">',而不是 'se\">'。 其中任何一个都可以工作:

str.IndexOf(@"se"">");  // verbatim string; escape quotation mark by doubling it
str.IndexOf("se\">");   // regular string; escape quotation mark using backslash

Your code is in fact searching for 'se\\">'. When searching for strings including backslashes I usually find it easier to use verbatim strings:

str.IndexOf(@"se\"">")

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 on str 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)

" '<em
class=\"correct_response\">a
night light</em><br
/><br /><table
width=\"100%\"><tr><td
class=\"right\">Ingrid</td></tr></table>')"

...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:

str.IndexOf(@"se"">");  // verbatim string; escape quotation mark by doubling it
str.IndexOf("se\">");   // regular string; escape quotation mark using backslash
风柔一江水 2024-08-02 00:03:51

这有效:

string str = "<case\\\">";
int i = str.IndexOf("se\\\">"); // i = 3

也许您没有正确转义两个字符串之一?

编辑您正在搜索的字符串中有一对额外的\

This works:

string str = "<case\\\">";
int i = str.IndexOf("se\\\">"); // i = 3

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.

不一样的天空 2024-08-02 00:03:51

也许 str 变量实际上并不包含反斜杠。
可能只是当您在调试时将鼠标悬停在变量上时,调试器工具提示将显示转义字符。

例如,如果在此分配之后放置断点,

string str = "123\"456";

工具提示将显示 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

string str = "123\"456";

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

决绝 2024-08-02 00:03:51

以下代码:

public static void RunSnippet()
{
    string s = File.ReadAllText (@"D:\txt.txt");
    Console.WriteLine (s);
    int i = s.IndexOf("se\\\">");
    Console.WriteLine (i);
}

给出以下输出:

some text before se\"> some text after
17

似乎对我有用......

Following code:

public static void RunSnippet()
{
    string s = File.ReadAllText (@"D:\txt.txt");
    Console.WriteLine (s);
    int i = s.IndexOf("se\\\">");
    Console.WriteLine (i);
}

Gives following output:

some text before se\"> some text after
17

Seems like working to me...

梦境 2024-08-02 00:03:51
TextBox2.Text = TextBox1.Text.IndexOf("se\"">")

好像在VB中可以工作。

TextBox2.Text = TextBox1.Text.IndexOf("se\"">")

seems to work in VB.

浮光之海 2024-08-02 00:03:51

字符串中的双引号需要像 "" 一样指定 还可以考虑使用逐字字符串 - 所以一个例子是

var source = @"abdefghise\"">jklmon";
Console.WriteLine(source.IndexOf(@"se\"">"));  // returns 8

DoubleQuotes within a string need to be specified like "" Also consider using verbatim strings - So an example would be

var source = @"abdefghise\"">jklmon";
Console.WriteLine(source.IndexOf(@"se\"">"));  // returns 8
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文