2013-04-11 162 views
1

我遇到了字符串比较的一些问题,字符串是由Request.queryString接收的字符串和文件.resx中的一行。C#字符串比较不起作用

代码收到Request.queryString到一个名为q变量,然后它去一个函数来比较,如果线路中有q值:

 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>"); 
     } 

正如它在静态文件搜索,特殊字符必须是审议并seraching为:iber&ecirc;,即从q到来,与iber&#234;是从行来:Iberê例如,因为.Contains.IndexOf.LastindexOf是比较不返回true。

考虑到我已经尝试使用ResXResourceReader(无法通过Visual Studio找到),ResourceReader和ResourceManager(这些我无法通过要读取的路径设置静态文件)。


编辑:

问题解决了。有中SpecialChars一个实例,覆盖q值与EntitiesEncode方法

+4

所以你说的是字符串“iber ê”不等于“iber ê”?这是正确的 - 我很惊讶,你感到惊讶... – 2013-04-11 15:23:29

+1

我敢肯定,你将需要将数据转换为'byte []'并比较这些数据。 – 2013-04-11 15:24:01

回答

3

的问题是ê字符在两个字符串逃过一劫。所以,如果你做了这样的事情,它不会工作:

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

你需要隐藏字符串。在System.Web组件中使用HttpUtility。这将工作:

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

如建议通过以下@ r3bel,如果你使用.NET 4级或以上,你也可以使用System.Net.WebUtility.HtmlDecode,所以你并不需要一个额外的程序集引用。

+2

写在这里:http://stackoverflow.com/questions/122641/how-can-i-decode-html-characters-in-c,你可以在.NET 4.0+中使用WebUtility.HtmlDecode :) – r3bel 2013-04-11 15:30:29

+0

@ r3bel The好的一点是你不需要对'System.Web.dll'的程序集引用。 – 2013-04-11 15:35:49

+0

@caerolus - 谢谢你的回答。我尝试解码,但'HttpUtility.HtmlDecode(q)= iber ê'和'HttpUtility.HtmlDecode(line)= iber ê'。 – 2013-04-11 15:56:56