C#unicode转字符串
本帖最后由 遗憾迟香 于 2020-3-24 16:13 编辑我从网上爬到了这种中英混合的字符串,中文部分用unicode表示了,而英文部分保持保持不变
示例:https://music.zhuolin.wang/api.php?callback=jQuery11130182920795747622_1585018563804&types=search&count=20&source=netease&pages=1&name=%E9%B8%A1%E4%BD%A0%E5%A4%AA%E7%BE%8E
爬到的部分字符串为:
\u8521\u5f90\u5764-\u53ea\u56e0\u4f60\u592a\u7f8e\uff08ROE remix\uff09
我把获取网页源码的代码的StreamReader编码改成Encoding.Unicode之后啥都没了,请问怎么解决?
自从学了编程之后,越来越讨厌中文了
/// <summary>
/// 获取网页源码
/// </summary>
/// <param name="url">url</param>
/// <returns></returns>
public static string GetS(string url, Encoding en)
{
string message = string.Empty;
HttpWebRequest request;
try
{
request = (HttpWebRequest)WebRequest.Create(url);
request.Accept = "text/html, application/xhtml+xml, */*";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)";
request.KeepAlive = true;
request.ProtocolVersion = HttpVersion.Version11;
ServicePointManager.Expect100Continue = false;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream, en);
message = reader.ReadToEnd();
reader.Close();
responseStream.Close();
}
catch (Exception err)
{
message = err.Message;
}
return message;
} 简单点的笨方法,转一下就是了。
```
private void Button1_Click(object sender, EventArgs e)
{
var url = "https://music.zhuolin.wang/api.php?callback=jQuery11130182920795747622_1585018563804&types=search&count=20&source=netease&pages=1&name=%E9%B8%A1%E4%BD%A0%E5%A4%AA%E7%BE%8E";
var text = GetS(url,Encoding.UTF8);
//Unicode转中文
text = Unicode2String(text);
richTextBox1.Text = text;
}
public static string Unicode2String(string source)
{
return new Regex(@"\\u({4})", RegexOptions.IgnoreCase | RegexOptions.Compiled).Replace(
source, x => string.Empty + Convert.ToChar(Convert.ToUInt16(x.Result("$1"), 16)));
}
```
果然是大佬 改成UTF-8呗
页:
[1]