首页 > 编程 > C# > 正文

c#字符长度查询代码

2020-01-24 03:41:46
字体:
来源:转载
供稿:网友
复制代码 代码如下:

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
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表