c#获取字符串宽度的示例代码(字节数方法)
/**//// /// /// /// /// ///
private arraylist getseparatesubstring(string morigianlstring, int substringcharnumber)
{
arraylist resultlist = new arraylist();
string tempstr = morigianlstring;
int charnumber = substringcharnumber;
int totalcount = 0;
string msubstr = "";
for (int i = 0; i < tempstr.length; i++)
{
string mchar = tempstr.substring(i, 1);
int bytecount = encoding.default.getbytecount(mchar);
if (bytecount == 1)
{
totalcount++;
msubstr += mchar;
if (totalcount == charnumber || i == tempstr.length - 1)
{
resultlist.add(msubstr);
totalcount = 0;
msubstr = "";
}
}
else if (bytecount > 1)
{
totalcount += 2;
if (totalcount > charnumber )
{
resultlist.add(msubstr);
if (i == tempstr.length - 1)
{
msubstr = mchar;
resultlist.add(msubstr);
}
else
{
totalcount = 2;
msubstr = mchar;
}
}
else if (totalcount == charnumber)
{
msubstr += mchar;
resultlist.add(msubstr);
totalcount = 0;
msubstr = "";
}
else if (i == tempstr.length - 1)
{
msubstr += mchar;
resultlist.add(msubstr);
}
else
{
msubstr += mchar;
}
}
}
return resultlist;
}
上一篇: 在.NET中取得代码行数的方法
下一篇: .net 添加Cookie的4种方法