分割字符串--包括中文

public static string [] SubstringByByte(string str, int byteLength)
        {
            char[] chars = str.ToCharArray();           
            string str1 = null;
            string str2 = null;
            for (int i = 0; i < chars.Length; i++)
            {
                char temp = chars[i];
                byte[] bytes = System.Text.ASCIIEncoding.Default.GetBytes(temp.ToString());
                if (byteLength >= bytes.Length)
                {
                    str1 += temp.ToString();
                    byteLength = byteLength - bytes.Length;
                }
                else
                {
                    str2 = str.Substring(i);
                    break;
                }
            }
            return new string[] { str1, str2 };
        }
posted @ 2010-03-11 14:59 Netprawn 阅读(28) 评论(0) 编辑 收藏


Locations of visitors to this page