Chr*_*ian 10 c# string string-length
我看到这个奇怪的问题,在网上任何地方都找不到类似的东西:
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
Run Code Online (Sandbox Code Playgroud)
我在各种C#项目中尝试了这一点,甚至要求另一位开发人员确认在不同机器上的不同项目中的行为是相同的.
我在VB.NET中尝试了同样的方法:
Dim l As Integer = "K".Length 'This returns 1 correctly
Run Code Online (Sandbox Code Playgroud)
我失去了吗?
har*_*old 12
这是因为"K"与"K"不同,其中有一个不可见的字符,即ascii值为30(记录分隔符).
您可以通过执行验证
byte[] bytes = Encoding.ASCII.GetBytes("K");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
424 次 |
| 最近记录: |