当前位置: 编程技术>.net/c#/asp.net
c#字符长度查询代码
来源: 互联网 发布时间:2014-10-14
本文导语: 代码如下: 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.ASC...
代码如下:
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