我从一个网站上抓取了一些数据。数据中名为urlresult的字符串是"http:\/\/www.cnopyright.com.cn\/index .php?com=com_noticeQuery&method=wareList&optionid=1221&obligee=\u5317\u4eac\u6c83\u534e\u521b\u65b0\u79d1\u6280\u6709\u9650\u516c\u53f8&softwareType=1"
。
我想做的是去掉上面字符串urlresult中的前三个字符@'\'。我试过下面的功能:
public string ConvertDataToUrl(string urlresult )
{
var url= urlresult.Split('?')[0].Replace(@"\", "") + "?" + urlresult .Split('?')[1];
return url
}
它返回 "http://www.cnopyright.com.cn/index.php?com=com_noticeQuery&method=wareList&optionid=1221&obligee=\\u5317\\u4eac\\u6c83\\u534e\\u521b\\u65b0\\u79d1\\u6280\\u6709\\u9650\\u516c\\u53f8&softwareType=1"
这是不正确的。
正确的结果是"http://www.cnopyright.com.cn/index.php?com=com_noticeQuery&method=wareList&optionid=1221&obligee=北京沃华创新科技有限公司&softwareType=1"
我试过很多方法,但都没有用,我不知道如何得到正确的结果。
最佳答案
我认为您可能被调试器误导了,因为您提供的代码没有理由插入额外的“\”字符。通常,调试器会在带引号的字符串中显示额外的“\”,以便您可以分辨出哪些“\”字符确实存在,哪些代表其他特殊字符。我建议使用 Debug.WriteLine 写出字符串或将其放入日志文件中。我认为您在问题中提供的信息不正确。
作为证明,我编译并运行了这段代码:
static void Main(string[] args)
{
var url = @"http:\/\/www.cnopyright.com.cn\/index.php?com=com_noticeQuery&method=wareList&optionid=1221&obligee=\u5317\u4eac\u6c83\u534e\u521b\u65b0\u79d1\u6280\u6709\u9650\u516c\u53f8&softwareType=1";
Console.WriteLine("{0}{1}{2}", url, Environment.NewLine,
url.Split('?')[0].Replace(@"\", "") + "?" + url.Split('?')[1]);
}
输出是:
http:\/\/www.cnopyright.com.cn\/index.php?com=com_noticeQuery&method=wareList&optionid=1221&obligee=\u5317\u4eac\u6c83\u534e\u521b\u65b0\u79d1\u6280\u6709\u9650\u516c\u53f8&softwareType=1
http://www.cnopyright.com.cn/index.php?com=com_noticeQuery&method=wareList&optionid=1221&obligee=\u5317\u4eac\u6c83\u534e\u521b\u65b0\u79d1\u6280\u6709\u9650\u516c\u53f8&softwareType=1
关于c# - 如何将 char @"\"转换为 Escape String\by C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46890332/