商讯信箱
用户名: @
密  码:   注册|忘记密码
登录
个人用户经销商
您的位置:首页 > 技术频道 > 正文

C#截取指定长度中英文字符串方法

作者:ITPUB论坛  2008-05-09

【IT168技术文档】

public static string GetFirstString(string stringToSub, int length) { Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled); char[] stringChar = stringToSub.ToCharArray(); StringBuilder sb = new StringBuilder(); int nLength = 0; bool isCut=false; for(int i = 0; i < stringChar.Length; i++) { if (regex.IsMatch((stringChar[i]).ToString())) { sb.Append(stringChar[i]); nLength += 2; } else { sb.Append(stringChar[i]); nLength = nLength + 1; } if (nLength > length) { isCut=true; break; } } if(isCut) return sb.ToString()+".."; else return sb.ToString(); }
1
【内容导航】
第1页: 概述
©版权所有。未经许可,不得转载。
[责任编辑:nancy]
[an error occurred while processing this directive]