Ricky
Ricky

Reputation: 35833

A string replacement question with C#

How do I remove string in bold within following url where q= is fixed parameter?

http://abc.com/qwe.aspx?q=DIEYeGJgNwvPSJ32ic1sY5x1ZYjOZTQZD9mjWl2EQJ8=&u=/foo/boo/kb

Thanks.

Upvotes: 1

Views: 218

Answers (5)

Matías Fidemraizer
Matías Fidemraizer

Reputation: 64933

Maybe this URL comes from some request, meaning you've that query string in the HttpRequest instance, associated to HttpContext.

In this case, you can always remove "q" query string parameter just by calling HttpContext.Current.Request.QueryString.Remove("q");

Another solution would be the one suggested by Alex.

Upvotes: 0

Oleks
Oleks

Reputation: 32323

it is pretty simple. I use System.Uri class to parse the url, then remove the q query string parameter, and then build a new url without this parameter:

var url = new Uri("http://abc.com/qwe.aspx?q=DIEYeGJgNwvPSJ32ic1sY5x1ZYjOZTQZD9mjWl2EQJ8=&u=/foo/boo/kb");
var query = HttpUtility.ParseQueryString(url.Query);
query.Remove("q");
UriBuilder ub = new UriBuilder(url);
ub.Query = query.ToString();
var result = ub.Uri.ToString();

Now result holds value: http://abc.com/qwe.aspx?u=/foo/boo/kb.

Upvotes: 10

Zebi
Zebi

Reputation: 8882

Are the positions fixed? You can do one IndexOf("q=") and IndexOf("u=") and use SubString twice to remove the part. An other way would be to use regular expressions.

Upvotes: 0

John M Gant
John M Gant

Reputation: 19308

input = Regex.Replace(input, "q=[^&]+", "") would be one way to do it.

Upvotes: 1

tster
tster

Reputation: 18237

if Q is a fixed parameter....

str = str.Replace("q=DIEYeGJgNwvPSJ32ic1sY5x1ZYjOZTQZD9mjWl2EQJ8=", "");

Otherwise I would do:

var qa = Request.QueryString;
qa.Remove("q");
var queryString = qa.ToString();

Upvotes: -1

Related Questions