0

I have this url http://spdata.com?value=1&rem=288&data=1 ..I want to replace the rem=288(this 288 is not static ) with some other value like membership=1 .Can someone tell me how to do this. Was thinking about doing a substring and then combining but not sure if there is an already easy process to do this .

Thanks

1
  • Are you building that url dynamically? Or do you have a big chunk of text with many urls in them that you need to replace? Commented Mar 13, 2014 at 13:42

5 Answers 5

2

You can do it with regular expressions.

Something like this:

using System.Text.RegularExpressions;
string newUrl= Regex.Replace("http://spdata.com?value=1&rem=288&data=1", @"rem=\d*", "membership=1",RegexOptions.IgnoreCase);
Sign up to request clarification or add additional context in comments.

2 Comments

Very nice and precise.
@Agustin Very Nice answer bro it Worth to be accepted answer and up vote :)
1

This should work for you.According to your url pattern, this will change all "rem=x" with another value.In this case it is "membership=1",ofcourse you can make it dynamic too.

var url = "http://spdata.com?value=1&rem=288&data=1";

var parts = url.Split('&').Select(x =>
        {
            if (x.Contains("rem=")) return "membership=1";
            return x;
        });

var result = string.Join("&", parts);

Comments

1

Try To use This

   string text = "membership=1";
   string x = "http://spdata.com?value=1&rem=288&data=1 ";
   int y = x.IndexOf("rem=");

   string z = x.Substring(y, x.Length - y);

   int a = z.IndexOf("&");

   string url = x.Substring(0, y) + text + z.Substring(a, z.Length - a);

Comments

0

If you want easy, quick and readable then why not just concatenate the strings together? Or use string.Format

string key="rem";
string value="288";
string.Format("http://spdata.com?value=1&{0}={1}&data=1", key, value)

Comments

0
Uri uri = new Uri("http://spdata.com?value=1&rem=288&data=1...")
var pairs = HttpUtility.ParseQueryString(uri.Query)
pairs.Remove("rem");
pairs.Add("membership", "1");

var builder = new UriBuilder(uri);
builder.Query = pairs.ToString();

Uri modified = builder.Uri;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.