title: How to efficiently remove a query string by Key from a Url?
body: <p>How to remove a query string by Key from a Url?</p>
  
  <p>I have the below method which works fine but just wondering is there any better/shorter way? or a built-in .NET method which can do it more efficiently?</p>
  
  <pre><code> public static string RemoveQueryStringByKey(string url, string key)
          {
              var indexOfQuestionMark = url.IndexOf("?");
              if (indexOfQuestionMark == -1)
              {
                  return url;
              }
  
              var result = url.Substring(0, indexOfQuestionMark);
              var queryStrings = url.Substring(indexOfQuestionMark + 1);
              var queryStringParts = queryStrings.Split(new [] {'&amp;'});
              var isFirstAdded = false;
  
              for (int index = 0; index &lt;queryStringParts.Length; index++)
              {
                  var keyValue = queryStringParts[index].Split(new char[] { '=' });
                  if (keyValue[0] == key)
                  {
                      continue;
                  }
  
                  if (!isFirstAdded)
                  {
                      result += "?";
                      isFirstAdded = true;
                  }
                  else
                  {
                      result += "&amp;";
                  }
  
                  result += queryStringParts[index];
              }
  
              return result;
          }
  </code></pre>
  
  <p>For example I can call it like:</p>
  
  <pre><code>  Console.WriteLine(RemoveQueryStringByKey(@"http://www.domain.com/uk_pa/PostDetail.aspx?hello=hi&amp;xpid=4578", "xpid"));
  </code></pre>
  
  <p>Hope the question is clear.</p>
  
  <p>Thanks,</p>