转换unicode不能在c#中工作

时间:2017-12-07 05:17:44

标签: c# unicode

我正在尝试在c#中转换unicode字符,但它无法正常工作

代码:

using System;
using System;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace test_request
{
    class MainClass
    {
        public static void Main (string[] args)
        {

            string unicode = "Not Your Boyfriend's Tunic";
            string str = System.Uri.UnescapeDataString(unicode);


            Console.WriteLine (str);
        }
    }
}

输出继电器:

Not Your Boyfriend's Tunic

但预期的输出是这个

预期产出:

Not Your Boyfriend's Tunic 

3 个答案:

答案 0 :(得分:3)

使用以下功能:

HttpUtility.HtmlDecode("Not Your Boyfriend's Tunic");
HttpUtility.HtmlEncode("Not Your Boyfriend's Tunic");

答案 1 :(得分:2)

这两种解决方案都适合您:

  1. 使用 Server.HtmlDecode

     string unicode = "Not Your Boyfriend's Tunic";
     string str = Server.HtmlDecode(unicode);
     Console.WriteLine (str);
    

    参考链接:This MSDN document

  2. 使用 HttpUtility.HtmlDecode : 喂

    string unicode = "Not Your Boyfriend's Tunic";    
    string str = HttpUtility.HtmlDecode(unicode);
    Console.WriteLine (str);
    

    参考链接:This MSDN document

  3. 两者之间的区别:

    两者都做同样的工作,但唯一的区别是:Server.HtmlDecode()在运行时很容易从网页上获得,而HttpUtility.HtmlDecode()是一个可以在任何地方使用的静态方法。

答案 2 :(得分:1)

试试这个

using System;
using System;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace test_request
{
    class MainClass
    {
        public static void Main (string[] args)
        {

            string unicode = "Not Your Boyfriend's Tunic";
            string str = System.Web.HttpUtility.HtmlDecode(unicode);
            Console.WriteLine (str);
        }
    }
}