C#字符串比较不起作用
我遇到了字符串比较的一些问题,字符串是由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ê
,即从q
到来,与iberê
是从行来:Iberê
例如,因为.Contains
,.IndexOf
或.LastindexOf
是比较不返回true。
考虑到我已经尝试使用ResXResourceReader(无法通过Visual Studio找到),ResourceReader和ResourceManager(这些我无法通过要读取的路径设置静态文件)。
编辑:
问题解决了。有中SpecialChars
一个实例,覆盖q
值与EntitiesEncode
方法
的问题是ê
字符在两个字符串逃过一劫。所以,如果你做了这样的事情,它不会工作:
string line = "sample iberê text";
string q = "iberê";
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
,所以你并不需要一个额外的程序集引用。
写在这里:http://*.com/questions/122641/how-can-i-decode-html-characters-in-c,你可以在.NET 4.0+中使用WebUtility.HtmlDecode :) – r3bel 2013-04-11 15:30:29
@ r3bel The好的一点是你不需要对'System.Web.dll'的程序集引用。 – 2013-04-11 15:35:49
@caerolus - 谢谢你的回答。我尝试解码,但'HttpUtility.HtmlDecode(q)= iber ê'和'HttpUtility.HtmlDecode(line)= iber ê'。 – 2013-04-11 15:56:56
所以你说的是字符串“iber ê”不等于“iber ê”?这是正确的 - 我很惊讶,你感到惊讶... – 2013-04-11 15:23:29
我敢肯定,你将需要将数据转换为'byte []'并比较这些数据。 – 2013-04-11 15:24:01