C# String.Length from Microsoft Documentation

谁说我不能喝 提交于 2019-12-02 08:49:59

You would have to prevent the interpretation of the literal by the compiler. This can be done with the @ prefix, like this:

var characters = @"abc\u0000def";

The Length property of this string will then return 12, but there will no longer be an actual unicode character in the string.

The C# compiler will replace \u0000 by a null byte. That means, at execution time you will simply have only 7 characters in your memory.

If you don't want the compiler to replace the special char, you have to escape the backslash in the first place:

string characters = "abc\\u0000def";
Console.WriteLine(characters.Length);    // Displays 12
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!