代碼如下:
using System;
using System.Text;
namespace StringLengthTest
{
class Program
{
static void Main(string[] args)
{
string strTmp = "abcd1234軟件包";
int i = Encoding.GetEncoding("GB2312").GetBytes(strTmp).Length;
int j = Encoding.Unicode.GetBytes(strTmp).Length;
int m = Encoding.ASCII.GetBytes(strTmp).Length;
int n = Encoding.Default.GetBytes(strTmp).Length;
int k = Encoding.UTF8.GetBytes(strTmp).Length;
Console.WriteLine(strTmp);
Console.WriteLine("GB2312: " + i);
Console.WriteLine("Unicode:" + j);
Console.WriteLine("ASCII: " + m);
Console.WriteLine("Default:" + n);
Console.WriteLine("UTF8: " + k);
Console.ReadKey();
}
}
}
運行結果:
abcd1234軟件包
GB2312: 14
Unicode:22
ASCII: 11
Default:14
UTF8: 17