1. 程式人生 > >json中含有Unicode的處理辦法 C#

json中含有Unicode的處理辦法 C#

com regex html pre ati end 進行 string 字符

原文:json中含有Unicode的處理辦法 C#

public static class StringExtension  
{      
    #region unicode 字符轉義  
    /// <summary>  
    /// 轉換輸入字符串中的任何轉義字符。如:Unicode 的中文 \u8be5  
    /// </summary>  
    /// <param name="str"></param>  
    /// <returns></returns>  
    public static string
UnicodeDencode(this string str) { if (string.IsNullOrWhiteSpace(str)) return str; return Regex.Unescape(str); } /// <summary> /// 將字符串進行 unicode 編碼 /// </summary> /// <param name="str"></param> /// <returns></returns>
public static string UnicodeEncode(this string str) { if (string.IsNullOrWhiteSpace(str)) return str; StringBuilder strResult = new StringBuilder(); if (!string.IsNullOrEmpty(str)) { for (int i = 0; i < str.Length; i++) { strResult.Append(
"\\u"); strResult.Append(((int)str[i]).ToString("x4")); } } return strResult.ToString(); } #endregion }

json中含有Unicode的處理辦法 C#