C# Unicode與中文互轉


中文轉Unicode:HttpUtility.UrlEncodeUnicode(string str); 
轉換后中文格式:"%uxxxx"  舉例:"柳_abc123"  轉換結果是:"%u67f3_abc123"

Unicode轉中文1:HttpUtility.UrlDecode(string str);
str格式:"%uxxxx" ,舉例:"%u67f3_abc123"

Unicode轉中文2:Regex.Unescape(string str);
str格式:"\uxxxx" ,舉例:"\u67f3_abc123"

 

================================

 符合js規則的轉換

        /// <summary>
        /// 中文轉unicode(符合js規則的)
        /// </summary>
        /// <returns></returns>
        public static string unicode_js_0(string str)
        {
            string outStr = "";
            string a = "";
            if (!string.IsNullOrEmpty(str))
            {
                for (int i = 0; i < str.Length; i++)
                {
                    if (Regex.IsMatch(str[i].ToString(), @"[\u4e00-\u9fa5]")) { outStr += "\\u" + ((int)str[i]).ToString("x"); }
                    else { outStr += str[i]; }
                }
            }
            return outStr;

        } 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM