C# NET 中英混合字符串截断实例

//中英混合字符串截断
public static string getStr(string s, int l)
{
string temp = s;
if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= l)
{
return temp;
}
for (int i = temp.Length; i >= 0; i--)
{
temp = temp.Substring(0, i);
if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= l - 3)
{
return temp + "...";
}
}
return "...";
}

C#中文字符截取函数
///str_value 字符
///str_len 要截取的字符长度
public string leftx(string str_value,int str_len)
{
int p_num = 0;
int i;
string New_Str_value = "";

if (str_value=="")
{
New_Str_value = "";
}
else
{
int Len_Num = str_value.Length;

//if (Len_Num < str_len)
//{
// str_len = Len_Num;
//}


for (i = 0;i<=Len_Num - 1; i++)
{
//str_value.Substring(i,1);
if (i >Len_Num) break;
char c = Convert.ToChar(str_value.Substring(i,1));
if (((int)c > 255) || ((int)c<0))
{
p_num = p_num + 2;

}
else
{
p_num = p_num + 1;

}

if (p_num >= str_len)
{

New_Str_value = str_value.Substring(0,i+1);

break;
}
else
{
New_Str_value = str_value;
}

}

}
return New_Str_value;
}

//数据的自动换行

<table>
<tr>
<td style="word-wrap:break-word;width=100">
绑定你的数据,会照原样显示,长度超过100就自动折行。
</td>
</tr>
</table>

原文地址:https://www.cnblogs.com/chaoa/p/2375265.html