如何将字符串[]转换为一个IDictionary<字符串,字符串&GT ;? [英] How to convert a String[] to an IDictionary<String, String>?

查看:105
本文介绍了如何将字符串[]转换为一个IDictionary<字符串,字符串&GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何转换的String [] 的IDictionary<字符串,字符串>



在指数0,2,4的值,......应键,因此在指数值1,3,5,......应的值。



例如:

 新[] {^ BI,connectORCL ^ CR,connectCR} 

=>

 新字典<字符串,字符串> {{^ BI,connectORCL},{^ CR,connectCR}}; 


解决方案

 词典<字符串,字符串> ArrayToDict(字符串[] ARR)
{
如果(arr.Length%2!= 0)
抛出新的ArgumentException(数组中不包含偶数项);
&字典LT;字符串,字符串>字典=新词典<字符串,字符串>();
的for(int i = 0; I< arr.Length / 2;我++)
{
串键= ARR [2 * i];
字符串值= ARR [2 * I + 1];
dict.Add(键,值);
}
返回字典;
}


How to convert a String[] to an IDictionary<String, String>?

The values at the indices 0,2,4,... shall be keys, and consequently values at the indices 1,3,5,... shall be values.

Example:

new[] { "^BI", "connectORCL", "^CR", "connectCR" }

=>

new Dictionary<String, String> {{"^BI", "connectORCL"}, {"^CR", "connectCR"}};

解决方案

Dictionary<string,string> ArrayToDict(string[] arr)
{
    if(arr.Length%2!=0)
        throw new ArgumentException("Array doesn't contain an even number of entries");
    Dictionary<string,string> dict=new Dictionary<string,string>();
    for(int i=0;i<arr.Length/2;i++)
    {
      string key=arr[2*i];
      string value=arr[2*i+1];
      dict.Add(key,value);
    }
    return dict;
}

这篇关于如何将字符串[]转换为一个IDictionary&LT;字符串,字符串&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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