截取汉字,字母,符号组成的字符串
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编码后,再重新计算长度,来比较截取
个人觉得正则式的较好,但我还没怎么研究就拿来主义的用了,
等有时间再研究
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编码后,再重新计算长度,来比较截取
个人觉得正则式的较好,但我还没怎么研究就拿来主义的用了,
等有时间再研究
相关文章推荐
- 截取汉字,字母,符号组成的字符串
- 截取字符串,汉字占两个字节,字母占一个字节
- php中按字节截取字符串方法,(汉字占两个字节,字母占一个字节,页面编码必须为utf-8)
- VC 判断CString字符串中各位是数字,大小写字母,符号,汉字
- 截取字符串,汉字占两个字节,字母占一个字节
- php---------正则判断字符串中是否由汉字 数字 英文字母组成
- 截取字符串方法总结(区分汉字、数字、字母)
- VC 判断CString字符串中各位是数字,大小写字母,符号,汉字。(UNICODE和ANSI)
- Java模块 -- String字符串操作(数字,汉字,特殊符号过滤/截取)
- VC 判断CString字符串中各位是数字,大小写字母,符号,汉字
- 截取汉字字母混合的字符串方法
- 截取字符串,汉字占两个字节,字母占一个字节
- java判断字符串是否全部由数字,大小写字母,特殊符号组成
- php---------正则判断字符串中是否由汉字 数字 英文字母组成
- VC 判断CString字符串中各位是数字,大小写字母,符号,汉字。(UNICODE和ANSI)
- 截取字符串,汉字占两个字节,字母占一个字节
- 截取字符串,含有汉字和字母,不能截半个汉字
- 判断CString字符串中各位是数字,大小写字母,符号,汉字.xml
- VC 判断CString字符串中各位是数字,大小写字母,符号,汉字。(UNICODE和ANSI)
- 截取字符串,汉字占两个字节,字母占一个字节