I ran into some malicious requests while parsing log files and found this case where I threw an exception because I try to extract the non-querystring portion of the URL by taking everything up to the "?" if present:
string s = @"/adServer/adSer�1X���L) [���0�:�^�?ް�9� �a�d�t��h� I����PA� �1 �G���$�";
string ch = "?";
bool b = s.Contains(ch);
int i = s.IndexOf(ch);
But in this case, it contained a "?", but when I tried .Substring(0, IndexOf("?"))
I got an exception because IndexOf
was -1. How is this possible?
Here's the actual string in case it doesn't render properly:
So when this code runs, b == True
, but i == -1
.
Interstingly, if I change the code to char ch = '?'
instead of string ch = "?"
it works as expected.