我不明白为什么EndsWith返回false。
我有C#代码:
string heading = "yakobusho";
bool test = (heading == "yakobusho");
bool back = heading.EndsWith("sho");
bool front = heading.StartsWith("yak");
bool other = "yakobusho".EndsWith("sho");
Debug.WriteLine("heading = " + heading);
Debug.WriteLine("test = " + test.ToString());
Debug.WriteLine("back = " + back.ToString());
Debug.WriteLine("front = " + front.ToString());
Debug.WriteLine("other = " + other.ToString());输出为:
heading = yakobusho
test = True
back = False
front = True
other = TrueEndsWith是怎么回事?
发布于 2019-03-07 22:42:48
第三行中的"sho"字符串以zero length space开头。"sho".Length返回4,而((int)"sho"[0])返回8203,即零长度空间的Unicode值。
您可以使用它的十六进制代码在字符串中键入它,例如:
"\x200Bsho"令人恼火的是,该字符不被视为空白,因此不能使用String.Trim()删除它。
发布于 2019-03-07 22:35:55
这在"sho“字符串之前包含一个不可见的字符:
bool back = heading.EndsWith("sho");更正后的行:
bool back = heading.EndsWith("sho");发布于 2019-03-07 22:46:16
在EndsWith参数中有一个特殊字符。
正如您从这段代码中看到的:
class Program
{
static void Main(string[] args)
{
string heading = "yakobusho";
string yourText = "sho";
bool back = heading.EndsWith(yourText);
Debug.WriteLine("back = " + back.ToString());
Debug.WriteLine("yourText length = " + yourText.Length);
string newText = "sho";
bool backNew = heading.EndsWith(newText);
Debug.WriteLine("backNew = " + backNew.ToString());
Debug.WriteLine("newText length = " + newText.Length);
}
}输出:
back = False
yourText length = 4
backNew = True
newText length = 3yourText的长度是4,所以这个字符串中有一些隐藏的字符。
希望这能有所帮助。
https://stackoverflow.com/questions/55046203
复制相似问题