Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split out ints from string

Let's say I have a web page that currently accepts a single ID value via a url parameter:
http://example.com/mypage.aspx?ID=1234

I want to change it to accept a list of ids, like this:
http://example.com/mypage.aspx?IDs=1234,4321,6789

So it's available to my code as a string via context.Request.QueryString["IDs"]. What's the best way to turn that string value into a List<int>?

Edit: I know how to do .split() on a comma to get a list of strings, but I ask because I don't know how to easily convert that string list to an int list. This is still in .Net 2.0, so no lambdas.

like image 888
Joel Coehoorn Avatar asked Sep 15 '08 14:09

Joel Coehoorn


1 Answers

No offense to those who provided clear answers, but many people seem to be answering your question instead of addressing your problem. You want multiple IDs, so you think you could this this:

http://example.com/mypage.aspx?IDs=1234,4321,6789

The problem is that this is a non-robust solution. In the future, if you want multiple values, what do you do if they have commas? A better solution (and this is perfectly valid in a query string), is to use multiple parameters with the same name:

http://example.com/mypage.aspx?ID=1234;ID=4321;ID=6789

Then, whatever query string parser you use should be able to return a list of IDs. If it can't handle this (and also handle semi-colons instead of ampersands), then it's broken.

like image 174
Ovid Avatar answered Oct 01 '22 03:10

Ovid