c#字符长度查询代码_C#教程

分享到:

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
昵    称:
验证码: