Sajee
Sajee

Reputation: 4457

.NET string IndexOf unexpected result

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.

Upvotes: 2

Views: 3477

Answers (7)

Fredrik Mörk
Fredrik Mörk

Reputation: 158389

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

Upvotes: 8

Henk Holterman
Henk Holterman

Reputation: 273784

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

Upvotes: 1

Kyriacos
Kyriacos

Reputation: 93

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

Upvotes: 2

Gishu
Gishu

Reputation: 136663

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

Upvotes: 1

Lazy Bob
Lazy Bob

Reputation: 449

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

seems to work in VB.

Upvotes: 1

Paolo Tedesco
Paolo Tedesco

Reputation: 57282

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.

Upvotes: 2

Hemant
Hemant

Reputation: 19846

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

Upvotes: 1

Related Questions