Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IndexOf fails when there is a backslash in the string -- Why?

Tags:

string

c#

Why does IndexOf fail when there is a backslash "\" in the string?

string tmpString = "acg2xs5d.dui";

string tmpString2 = @"c:\acg2xs5d.dui"; 

MessageBox.Show(tmpString.IndexOf(@tmpString2).ToString());


This returns -1; no matter what.

If I change tmpString2 to "acg2xs5d.dui" and remove the "c:\" it returns 0 as expected.

It seems that the "\" is causing it to give an incorrect result of "-1".

Why is this and how do I work around/trap for this?

like image 310
fraXis Avatar asked Dec 09 '22 22:12

fraXis


1 Answers

you need just to use tmpString2.IndexOf(@tmpString)

instead of tmpString.IndexOf(@tmpString2)

all is right except of the order. "\" is not an escape character because you use "@" before your string.

like image 114
Dmitry Khryukin Avatar answered Mar 02 '23 15:03

Dmitry Khryukin