1

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user1770094
  • 87
  • 1
  • 3
  • 8
  • Could `Split` on the name and then skip 1 character to get start of number. In that case, you either end at '&' or EOS. – DonBoitnott Jan 23 '14 at 20:37
  • 1
    Did you check out `string.Replace`? – Will Custode Jan 23 '14 at 20:38
  • 3
    Better yet: http://stackoverflow.com/questions/659887/get-url-parameters-from-a-string-in-net. Then just use `Get("Steve")`. – DonBoitnott Jan 23 '14 at 20:39
  • Possibly significant for answers: did you purposely leave out the "?" mark indicating the start of the query string? That is, should the URL not be `"http://www.somesite.com/?John=1849282&Steve=19847274&Lenny=1234567&Craig=1432659"` with `"?John"` instead? – Chris Sinclair Jan 23 '14 at 20:39
  • @ChrisSinclair no, I just used this example I'm using as a test for the time being until I get this thing to function properly – user1770094 Jan 23 '14 at 20:44
  • 1
    @user1770094: Then so long as you do include the "?" as part of your URL, DonBoitnott's comment/answer is probably the best way of going about it, otherwise you may run into some corner cases. For example, if you have "Roberta" and "Robert", and you search for "Robert", you may end up pulling out "Roberta" instead. (Similar issue for "MaryAnne" and "Anne") You'll also have issues with case-sensitivity which is avoided with DonBoitnott's solution. – Chris Sinclair Jan 23 '14 at 20:59

1 Answers1

1

The second parameter of substring is the length of the string you wish to pull out. Not the position of the last character. Try this

string url = textBox2.Text + "&";

int startPos = url.LastIndexOf(textBox1.Text) + textBox1.Text.Length + 1;
string firstPartRemoved = url.Substring(startPos, url.Length - startPos);
int locationOfAnd = firstPartRemoved.IndexOf("&");
string sub = firstPartRemoved.Substring(0,locationOfAnd)

Clipboard.SetText(sub);

But this would be loads easier with regular expressions

bennie j
  • 729
  • 4
  • 8