Reputation: 28336
I have a string representation of a URL like this:
http://www.GoodStuff.xxx/services/stu/query?where=1%3D1&text=&objectIds=231699%2C232002%2C231700%2C100646&time=
It's a URL but a string object in my code. How do I change the value of objectIds
? Do I need to find the string, objectIds
, then locate the &
before and after, and replace the contents with the desired value? Or is there a better way?
This is a .NET 4.5 FW console app...
Upvotes: 2
Views: 124
Reputation: 9863
If you're trying to replace values that are already in there it gets a little more tricky. Try this.
//Base URL. Doesn't need to be hardcoded. As long as it contains "objectIds=" then it will work
static string url = @"http://www.GoodStuff.xxx/services/stu/query?where=1%3D1&text=&objectIds=231699%2C232002%2C231700%2C100646&time=";
static void Main(string[] args)
{
//Get the start index
// +10 because IndexOf gets us to the o but we want the index of the equal sign
int startIndex = url.IndexOf("objectIds=") + 10;
//Figure out how many characters we are skipping over.
//This is nice because then it doesn't matter if the value of objectids is 0 or 99999999
int endIndex = url.Substring(startIndex).IndexOf('&');
//Cache the second half of the URL
String secondHalfOfURL = url.Substring(startIndex + endIndex);
//Our new IDs to stick in
int newObjectIDs = 12345;
//The new URL.
//First, we get the string up to the equal sign of the objectIds value
//Next we put our IDS in.
//Finally we add on the second half of the URL
String NewURL = url.Substring(0, startIndex) + newObjectIDs + secondHalfOfURL;
Console.WriteLine(NewURL);
Console.Read();
}
It ain't pretty but it does the work.
Upvotes: 1
Reputation: 727047
If the rest of the URL is fixed, you could locate the id manually, and use string.Format
and string.Join
to insert the IDs into it:
var urlString = string.Format(
"http://www.GoodStuff.xxx/services/stu/query?where=1%3D1&text=&objectIds={0}&time="
, string.Join("%", ids)
);
This will insert a %
-separated list of ids
from your code into the URL template.
Upvotes: 1