c# - 如何将 char @"\"转换为 Escape String\by C#

标签 c# escaping

我从一个网站上抓取了一些数据。数据中名为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/

相关文章:

c++ - 一个关于 C++ 中的 iostream cout 的问题

windows - 怎么逃!在变量的值中?

java - java中的转义引号

sql - 如何在类似字符串的sql中转义多个字符?

c# - 无法在 Azure 中创建带有正斜杠的容器

c# - URL 重写不起作用

c# - 我在 SQL 中发现一对(零或一) Entity Framework 关系的重复行 - 如何/为什么?

c# - 接口(interface)属性名称因类而异

c# - 存储库模式只能与 Entity Framework 一起使用吗?

c++ - 引起问题的反斜杠c++