Dim encodingData() As Byte dim str as string encodingData = System.Text.Encoding.Default.GetBytes(txtlength.Text) c = encodingData.Length.ToString str=System.Text.Encoding.Default.GetString(encodingData,0,20)
public static int GetStrLen(string Str) { int i = 0; foreach (char Char in Str) { if ((int)Char > 127) i += 2; else i ++; } return i; }
public static int GetStrLen(string Str) { int i = 0; foreach (char Char in Str) { if ((int)Char > 127) i += 2; else i ++; } return i; }
dim str as string encodingData = System.Text.Encoding.Default.GetBytes(txtlength.Text)
c = encodingData.Length.ToString
str=System.Text.Encoding.Default.GetString(encodingData,0,20)
{
int i = 0;
foreach (char Char in Str)
{
if ((int)Char > 127)
i += 2;
else
i ++;
}
return i;
}
{
int i = 0;
foreach (char Char in Str)
{
if ((int)Char > 127)
i += 2;
else
i ++;
}
return i;
}
Byte[] encodingData = System.Text.Encoding.Default.GetBytes(mystr);
mystr = System.Text.Encoding.Default.GetString(encodingData,0,10);
this.Label1.Text = mystr;
重新写了一个函数如下:/// <summary>
/// 截取以字节计算的文本长度
/// </summary>
/// <param name="msytr">要截取的文本串</param>
/// <param name="intLength">要截取的字节数</param>
/// <returns>返回截取的字节数</returns>
private string myCutString(string mystr,int intLength)
{
Byte[] myByte = System.Text.Encoding.Default.GetBytes(mystr);
string str = "";
if(myByte.Length < intLength)
{
str = mystr;
}
else
{
str = System.Text.Encoding.Default.GetString(myByte,0,intLength);
switch(System.Text.Encoding.Default.GetByteCount(mystr) - intLength)
{
case 1:
str += "..";
break;
case -1:
str += "...";
break;
default:
break;
}
}
return(str);
}调用:
string mystr = "Ennc";
this.Label1.Text = this.myCutString(mystr,8);
{
case 1:
str += "..";
break;
case -1:
str += "...";
break;
default:
break;
}
}
这儿有点不明白,你是不是要一个"."代表不足一位