从“ 1,2,3”解析int []

问题描述

| 我有一个名为ID的多选下拉列表,它提交ID = 1,2,3,我需要将其解析为一个整数数组以在filter方法中执行Contains()。目前,我使用:
string[] ids = Request[\"ID\"].Split(\',\');
我真的不喜欢它,因为它比int慢比较字符串。有什么建议么?     

解决方法

Request[\"ID\"].Split(\',\').Select(x=>int.Parse(x)).ToArray();
当然,如果任何所得的数字字符串不是\“ parseable \”(是否存在这样的单词?),则将抛出此错误。     ,如果转换为整数更快或字符串比较更快,则取决于在数组中查找的次数。
HashSet<int> ids = new HashSet<int>(from s in Request[\"ID\"].Split(\',\')
                                    select int.Parse(s));
但是如果您有许多id:s,最快的方法可能是创建
HashSet<string>
HashSet<string> = new HashSet<string>(string[] ids = Request[\"ID\"].Split(\',\'));
    ,
int[] ids = Request[\"ID\"].Split(\',\').Select(Convert.ToInt32).ToArray();
    ,第一:
string[] arr = Request[\"ID\"].Split(\',\')
然后:
Array.ConvertAll(arr,s => Int32.Parse(s));
Array.ConvertAll(arr,Int32.Parse);

arr.Select(s => Int32.Parse(s));
arr.Select(Int32.Parse);
然后:
new HashSet<int>(result);
(最快执行perform9 container的容器) 也可以看看: 使用LINQ在一个字符串中将string []转换为int [] int.Parse(),int.TryParse(),Convert.Int32()中哪一个处理和转换速度更快     ,如果您没有linq,则可以:
string[] ids = Request[\"ID\"].Split(\',\');
int[] items = new int[ids.Length];
for(int i = 0; i<ids.Length; i++)
{
  items[i] = int.Parse(ids[i]);
}
    

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...