1. 程式人生 > >ASCII字串解碼,比如Ӓӓ

ASCII字串解碼,比如Ӓӓ

用正則分別解碼字串裡可以解碼的ASCII字元

/// <summary>
/// ASCII解碼
/// </summary>
/// <param name="str">需要解碼的字串</param>
/// <returns></returns>
public static string ToAsciiIDecode(this string str)
{
    StringBuilder sb = new StringBuilder();
    int indexSatae = -1;
    foreach (Match i in new Regex(@"&#\d{1,5};").Matches(str))
    {
        string s = i.Value.Substring(2);
        sb.Append((char)(s.Substring(0,s.Length-1).toInt()));
        if (indexSatae != -1)
        {
            sb.Append(str.Substring(indexSatae, i.Index - indexSatae));
        }
        indexSatae = i.Index + i.Length;
    }
    if (indexSatae != -1)
    {
        sb.Append(str.Substring(indexSatae));
    }
    return sb.ToString();
}
 
/// <summary>
/// 轉換成Int型別
/// </summary>
/// <param name="str">需要轉換的字串</param>
/// <returns></returns>
public static int toInt(this string str)
{
    return int.Parse(str);
}