拆分从字符串整数 [英] Split out ints from string

查看:252
本文介绍了拆分从字符串整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有,目前接受通过URL参数有关的ID值网页:
     http://example.com/mypage.aspx?ID=1234

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

我想改变它接受列表中的的IDS,就像这样:
     http://example.com/mypage.aspx?IDs=1234,4321,6789

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

因此​​,它提供给我的code为通过的 context.Request.QueryString [编号]字符串的是什么把该字符串值转换成一个名单,其中最好的办法; int>的?

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>?

编辑:我知道该怎么做.split()上一个逗号,以获得一个字符串列表,但我问,因为我不知道如何轻松的字符串列表转换为int名单。这仍然是.NET 2.0中,所以没有lambda表达式。

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.

推荐答案

没有冒犯那些谁提供了明确的答案,但很多人似乎是回答你的问题,而不是解决你的问题。你要多个ID,所以你觉得你会这样的:

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:

<一个href="http://example.com/mypage.aspx?IDs=1234,4321,6789">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:

<一个href="http://example.com/mypage.aspx?ID=1234;ID=4321;ID=6789">http://example.com/mypage.aspx?ID=1234;ID=4321;ID=6789

然后,无论查询字符串解析器使用应该能够返回ID列表。如果它不能处理这种情况(甚至处理分号,而不是&符号),那么它的坏了。

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.

这篇关于拆分从字符串整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆