【发布时间】:2013-03-09 11:31:54
【问题描述】:
我看到了这个奇怪的问题,在网络上的任何地方都找不到类似的东西:
int l = "K".Length; //This actually returns 2 !!! The 'Autos' window in
//the debugger also shows "K".Length as 2.
string s = "K";
l = s.Length; //Whereas this returns 1 as expected
我在各种 C# 项目中尝试过这个,甚至要求另一位开发人员确认在不同机器上的不同项目中的行为是相同的。
我在 VB.NET 中也尝试过:
Dim l As Integer = "K".Length 'This returns 1 correctly
我会失去它吗?
【问题讨论】:
-
调试器的 quickwatch 的
"K".Length显示 1。 -
调试器的即时
"K".Length显示1。 -
我认为你正在失去它。您的字符串长度为 2 的原因是它包含两个字符。
-
第一个例子中K前面不是空格吗? :)
-
您希望第一个带有特殊字符的返回
1?
标签: c# string string-length