开发者

Is there a way to check the encoding of a C# string? [duplicate]

This question already has answers here: Closed开发者_Go百科 11 years ago.

Possible Duplicate:

Determine a string's encoding in C#

I believe if I create a string it defaults to UTF8, however if the string is created else where and I want to be extra safe before dealing with it and check what its encoding is I do not see any easy way to do that using the string or Encoding class. Am I missing something or is a C# string always UTF8 no matter what?


Strings in C# (well, .NET) don't have encoding, effectively... or you can view them all as UTF-16, given that they're a sequence of char values, which are UTF-16 code units.

Normally, however, you only need to care about encoding when you convert from a string to a binary form (e.g. down a socket or to a file). At that point, you should specify the encoding explicitly - the string itself has no concept of this.

The only aspect which "defaults" to UTF-8 is that there are plenty of .NET APIs which are overloaded to either accept an encoding or not, and if no encoding is specified, UTF-8 is used. File.ReadAllText is an example of this. However, after reading the file there's no distinction between "text which was read from a UTF-8 file" and "text which was read from a Big5 file" etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜