Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# string.replace不为我工作_C#_Asp.net_String - Fatal编程技术网

C# string.replace不为我工作

C# string.replace不为我工作,c#,asp.net,string,C#,Asp.net,String,我有以下代码: string encodedKeywordsQueryValue = HttpUtility.UrlEncode(HttpContext.Current.Request.QueryString["keywords"]); query = query.Replace(HttpContext.Current.Request.QueryString["keywords"], encodedKeywordsQueryValue); 在哪里 及 我的旧字符串值不会被新字符串值替换 有人能

我有以下代码:

string encodedKeywordsQueryValue = HttpUtility.UrlEncode(HttpContext.Current.Request.QueryString["keywords"]);
query = query.Replace(HttpContext.Current.Request.QueryString["keywords"], encodedKeywordsQueryValue);
在哪里

我的旧字符串值不会被新字符串值替换

有人能建议我这样做吗?我尝试了字符串的删除和插入方法的组合,但没有得到想要的结果


请帮我解决这个问题。

您应该在“替换”的左侧有一个精确的字符串。它的行为不像“喜欢”。请参见下面的示例:

string str1 = "abc";
string str2 = "abc&def";
str2.replace("d", str1);
结果将是:

abc&abcef
希望能有帮助

HttpContext.Current.Request.QueryString[“关键字”]=“abc&abc”和 encodedKeywordsQueryValue=abc%26+abc

阅读replace方法的文档会告诉您,它不知道URL编码,也不关心—它是一个字符串方法。所以,在未编码的字符串上工作。没有类似的风格行为-这是一个完全“匹配和替换”


我建议你为这种行为做一个单元测试,然后确定你希望它是怎样的-然后你可以在这里写一篇文章,这篇文章在某种程度上是有用的,因为它会有一个行为不起作用的确切例子,这不是我们要解决的谜题。

查询变量的值是什么?这种方法自.NET 2.0以来就存在了。多年来,无数的开发者都在使用它。我认为在这样一个基本的操作中,你不会有意外的行为。
string str1 = "abc";
string str2 = "abc&def";
str2.replace("d", str1);
abc&abcef