欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#字符长度查询代码

程序员文章站 2023-10-23 18:58:04
复制代码 代码如下: using system; using system.text; namespace stringlengthtest { class program...
复制代码 代码如下:

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