在C#中截取指定长度的中文字符串

private static string SubString(string stringToSub, int length) {
            Regex regex 
= new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
            
char[] stringChar = stringToSub.ToCharArray();
            StringBuilder sb 
= new StringBuilder();
            
int nLength = 0;

            
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)
                    
break;
            }


            
return sb.ToString();
        }
posted @ 2004-10-26 23:20  V.Enjoy  阅读(1437)  评论(0编辑  收藏  举报