程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# NET 中英混合字符串截斷實例

C# NET 中英混合字符串截斷實例

編輯:C#入門知識

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved