I'm making a tiny program which is supposed to take a URL that is fed into a regular textbox(textBox2), and then let the user add a certain name or ID they are looking for in the URL into another texbox(textBox1), then finally the program is supposed to go through the URL, select the part of the URL between the ID and a "&", and copy it to the clipboard.
This is the code(I've put it inside a button in case you're wondering):
string url = textBox2.Text + "&";
int startPos = url.LastIndexOf(textBox1.Text) + textBox1.Text.Length + 1;
int length = url.IndexOf("&");
string sub = url.Substring(startPos, length);
Clipboard.SetText(sub);
So say I've got the URL: http://www.somesite.com/John=1849282&Steve=19847274&Lenny=1234567&Craig=1432659 Then I type into textBox1 "Steve", and put this entire URL into textBox2, then when I click the button it should copy this content to the clipboard: "19847274"
I realized that it might have some problems getting Craig's content, because there is no "&" at the end of it, so I made that new string called "url" to try and get past that for the time being.
But when I type in John it gives me this: 1849282&Steve=19847274&Lenny=1234567
And when I type in Steve it gives me this: 19847274&Lenny=1234567&Craig=1432659
And when I type in Lenny or Craig the program stops working and freezes, complaining about this:
Index and length must refer to a location within the string.
Parameter name: length
So problem nr.1 is that it copies beyond what it should, and problem nr.2 is that it doesn't add that & after Craig's content. Any suggestions or ideas on how to fix this? Or what I've done wrong? Is there a better way to go about with this? All advice is appreciated.