日期:2014-05-20 浏览次数:21039 次
bool Locked(string key) { if (string.Compare(key, "", StringComparison.InvariantCulture) != 0) return true; return key.Length == 0; }
bool Locked(string key) { if (string.Compare(key, "", StringComparison.InvariantCulture) != 0) return true; return key.Trim('\0').Length == 0; }
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.
但是我试验了下:
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 的值 !
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));