日期:2014-05-20  浏览次数:21039 次

C# 字符串比较问题
话说某国国防部的机密资料锁在一个保险柜里,需要输入一串密码才能打开该保险柜。
现在我方的间谍想办法搞到了该保险柜智能控制系统的关键源程序代码,如下:
C# code

bool Locked(string key)
{
  if (string.Compare(key, "", StringComparison.InvariantCulture) != 0) return true;
  return key.Length == 0;
}


只要输入适当的 key,使 Locked 方法返回 false 就能打开该保险柜,获取机密资料。
现在就看你的了,赶快给出答案吧。



-----------------------------------------
以下为版主修改的内容:
楼主在16楼和18楼把条件加强为:
C# code
bool Locked(string key)
{
  if (string.Compare(key, "", StringComparison.InvariantCulture) != 0) return true;
  return key.Trim('\0').Length == 0;
}


------解决方案--------------------
我已将答案通过短信息发给楼主了,希望其他破译出来的朋友也可以先这样做,免得答案太早泄露
------解决方案--------------------
key="/0" ?
------解决方案--------------------
key = "\0"; //转义符是反斜杠\
------解决方案--------------------
C# code


key="\u0BE6"

------解决方案--------------------
A similar oddity would be characters with no weight like U+FFFD. So if I have str = "A\xFFFD\xFFFD\xFFFD", then all of str.Substring(0,1) == str.Substring(0,2) == str.Substring(0,3) == str.Substring(0,4) == "A". And in this case str.StartsWith("A") would be true.
但是我试验了下:
C# code

string str2 = "A\xFFFD\xFFFD\xFFFD";
            string find2 = "A";

            Console.WriteLine("str2 = " + str2);
            Console.WriteLine("1  -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
            Console.WriteLine("Substring: " + (str2.Substring(0, 1) == find2) + "   Substring is:" + str2.Substring(0, 1));
            Console.WriteLine("StartsWith:" + str2.StartsWith(find2));
            Console.WriteLine("IndexOf:   " + str2.IndexOf(find2));

            Console.WriteLine("2  -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
            Console.WriteLine("Substring: " + (str2.Substring(0, 2) == find2) + "   Substring is:" + str2.Substring(0, 2));
            Console.WriteLine("StartsWith:" + str2.StartsWith(find2));
            Console.WriteLine("IndexOf:   " + str2.IndexOf(find2));

            Console.WriteLine("3  -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
            Console.WriteLine("Substring: " + (str2.Substring(0, 3) == find2) + "   Substring is:" + str2.Substring(0, 3));
            Console.WriteLine("StartsWith:" + str2.StartsWith(find2));
            Console.WriteLine("IndexOf:   " + str2.IndexOf(find2));

            Console.WriteLine("4  -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
            Console.WriteLine("Substring: " + (str2.Substring(0, 4) == find2) + "   Substring is:" + str2.Substring(0, 4));
            Console.WriteLine("StartsWith:" + str2.StartsWith(find2));
            Console.WriteLine("IndexOf:   " + str2.IndexOf(find2));

------解决方案--------------------
目前,所有没清楚本贴写的是什么的朋友, 试试下面这段代码, 跟踪一下,就该清楚了. 
如果还不清楚的话, 看看t 的值 ! 


C# code

string str1 = "Mu\x0308nchen";
string t = "x\x0308";
string str = "Muchen";
string find = "Mu";
Console.WriteLine("Substring: " + (str.Substring(0, 2) == find) + "   Substring is:" +str.Substring(0, 2));
Console.WriteLine("StartsWith:" + str.StartsWith(find));
Console.WriteLine("IndexOf:   " + str.IndexOf(find));