views:

422

answers:

6

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.

+2  A: 

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.

Paolo Tedesco
for me this work too: string str = "<case\">"; int i = str.IndexOf("se\">");
Fredou
doesn't work on this string: " '<em class=\"correct_response\">a night light</em><br /><br /><table width=\"100%\"><tr><td class=\"right\">Ingrid</td></tr></table>')"
Sajee
+1  A: 

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

Hemant
+1  A: 
TextBox2.Text = TextBox1.Text.IndexOf("se\"">")

seems to work in VB.

Lazy Bob
+7  A: 

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
Fredrik Mörk
This doesn't seem to work. Here's the output from VS 2008 immediate window.? answer" '<em class=\"correct_response\">a night light</em><br /><br /><table width=\"100%\"><tr><td class=\"right\">Ingrid</td></tr></table>')"? answer.IndexOf(@"se\"">")-1
Sajee
whoops, sorry about the formatting.
Sajee
Note that in the 'se\">' sequences that you see, the '\' character is only an escape character for the quotation mark. The string content is actually 'se">' without the backslash. str.IndexOf(@"se"">") should find what you seem to be looking for.
Fredrik Mörk
+2  A: 

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

Kyriacos
+1  A: 

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
Gishu