1

I'm having some issues with the string comparison of a string the is received by Request.queryString and a line from a file .resx.

The code receive Request.queryString to a variable named q, then it goes to a function to compare if a line has q value in it:

        while ((line = filehtml.ReadLine()) != null)
        {
            if (line.ToLower().Contains(q.ToLower().ToString()))
                HttpContext.Current.Response.Write("<b>Content found!</b>");
            else
                HttpContext.Current.Response.Write("<b>Content not found!</b>");
        }

As it's a search in static files, special characters must be consider and seraching for: Iberê for example, isn't returning true because the .Contains, .IndexOf or .LastindexOf is comparing: iber&ecirc;, that is coming from q, with iber&#234; that is coming from the line.

Consider that I already tried to use ResXResourceReader (which can't be found by Visual Studio), ResourceReader and ResourceManager (these I couldn't set a static file by the path to be read).


EDIT:

Problem solved. There was a instance of SpecialChars, overwriting q value with EntitiesEncode method

Fabiano Araujo
  • 876
  • 6
  • 19

1 Answers1

3

The problem is that the ê character is escaped in both strings. So if you did something like this, it wouldn't work:

        string line = "sample iber&ecirc; text";
        string q = "iber&#234;";
        if (line.Contains(q)) {
            // do something
        }

You need to unscape the strings. Use HttpUtility in the System.Web assembly. This will work:

        line = System.Web.HttpUtility.HtmlDecode(line);
        q = System.Web.HttpUtility.HtmlDecode(q);
        if (line.Contains(q)) {
            // do something
        }

As suggested by @r3bel below, if you're using .net 4 or above you can also use System.Net.WebUtility.HtmlDecode, so you don't need an extra assembly reference.

Julián Urbano
  • 8,378
  • 1
  • 30
  • 52
  • 2
    as written here: http://stackoverflow.com/questions/122641/how-can-i-decode-html-characters-in-c, you can use WebUtility.HtmlDecode in .NET 4.0+ :) – r3bel Apr 11 '13 at 15:30
  • @r3bel The good thing about that is that you don't need an assembly reference to `System.Web.dll`. – Jeppe Stig Nielsen Apr 11 '13 at 15:35
  • @caerolus - Thank you for your answer. I tried decode, but `HttpUtility.HtmlDecode(q) = iberê` and `HttpUtility.HtmlDecode(line) = iberê`. – Fabiano Araujo Apr 11 '13 at 15:56
  • @FabianoAraujo then your `q` and `line` must be something different. If yo try this `System.Net.WebUtility.HtmlDecode("iberê");` you get `iberê` as expected. If you do `Console.WriteLine(q)` what does it show in the console? – Julián Urbano Apr 11 '13 at 16:00
  • @caerolus - I'm not sure where should `Console.WriteLine(q)` show the result, but if I try `Response.Write(q)` it shows me `iberê` – Fabiano Araujo Apr 11 '13 at 16:22
  • maybe you are just unknowingly escaping back after `HtmlDecode`. Try debugging, en execute this `string test = System.Net.WebUtility.HtmlDecode("iberê");`. Variable `test` should be `iberê`. So if your `q` really is `iberê`, doing `string test = System.Net.WebUtility.HtmlDecode(q);` must work as well. Seriously, it works, there must be something wrong with how you check your string values. – Julián Urbano Apr 11 '13 at 16:43
  • @caerolus - test in this case returns `iberê`, you are right. I solved my problem checking each char in `q` and converting it to it's entity number, with before, so `iberê` turns into `iberê` as the line from the file has it. Although it's not the best approach, probably. Thanks for your effort and attention. – Fabiano Araujo Apr 11 '13 at 16:51
  • @FabianoAraujo turns into `iberê` or `iberê`? – Julián Urbano Apr 11 '13 at 16:53