//中英混合字符串截斷
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>
摘自 hcstudio