1

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:

enter image description here

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.

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
powlette
  • 1,800
  • 20
  • 41
  • ?ް is treating as a single character...that is why ? is not found..Try finding ?ް instead of ? in the string ... you will get the index – Saad Abdullah Jan 07 '14 at 20:29

0 Answers0