您的位置:首页 > 其它

截取汉字,字母,符号组成的字符串

2007-01-25 15:31 501 查看
搜索网络解决方法如下,我选择的正则式的方法
1 正则式方法
资源:/article/4824305.html

public static string SubMixText( string text, int maxLength, string replace ) {
2 if (string.IsNullOrEmpty(text)) {
3 return string.Empty;
4 } else {
5 string strReturn = "";
6 string strTemp = text;
7
8 if (Regex.Replace(strTemp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= maxLength) {
9 strReturn = strTemp;
10 } else {
11 for (int i = strTemp.Length; i >= 0; i--) {
12 strTemp = strTemp.Substring(0, i);
13 if (Regex.Replace(strTemp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= maxLength) {
14 strReturn = strTemp + replace;
15 break;
16 }
17 }
18 }
19 return strReturn;
20 }
21
22 }

二,
http://www.csafe.cn/article.asp?id=1212
后一种方法 是通过
byte[] tempByte=System.Text.Encoding.Default.GetBytes
取得 AscII编码后,再重新计算长度,来比较截取

个人觉得正则式的较好,但我还没怎么研究就拿来主义的用了,
等有时间再研究
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐