C# 保留字符串中的中文

 /// <summary>
        /// 保留中文字符串
        /// </summary>
        /// <returns></returns>
        public static string RetainChineseString(this string str) 
        {
            StringBuilder chineseString = new StringBuilder();
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] >= 0x4E00 && str[i] <= 0x9FA5) //汉字
                {
                    chineseString.Append(str[i]);
                }
            }
            return chineseString.Length>0? chineseString.ToString():string.Empty;
        }

 

posted @ 2021-11-05 18:31  LuoCore  阅读(650)  评论(0编辑  收藏  举报