见下代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Encoding { class A { public string Mys = string.IsInterned("Hello"); } class Program { static void Main(string[] args) { string[] sarr = new string[]{"a","我"}; for (int i = 0; i < sarr.Length; i++) { string s = sarr[i]; Console.WriteLine(System.Text.Encoding.ASCII.GetBytes(s).Length); Console.WriteLine(System.Text.Encoding.Unicode.GetBytes(s).Length); Console.WriteLine(System.Text.Encoding.Default.GetBytes(s).Length); Console.WriteLine(System.Text.Encoding.UTF7.GetBytes(s).Length); Console.WriteLine(System.Text.Encoding.UTF8.GetBytes(s).Length); Console.WriteLine(System.Text.Encoding.UTF32.GetBytes(s).Length); Console.WriteLine("-------------------------------------------------------"); } Console.ReadLine(); } } }
结果: