Most elegant way to convert string array into a dictionary of strings

后端 未结 8 982
没有蜡笔的小新
没有蜡笔的小新 2020-12-08 18:39

Is there a built-in function for converting a string array into a dictionary of strings or do you need to do a loop here?

相关标签:
8条回答
  • 2020-12-08 19:12
                Dictionary<int, string> dictionaryTest = new Dictionary<int, string>();
    
                for (int i = 0; i < testArray.Length; i++)
                {
                    dictionaryTest.Add(i, testArray[i]);
                }
    
                foreach (KeyValuePair<int, string> item in dictionaryTest)
                {
    
    
                    Console.WriteLine("Array Position {0} and Position Value {1}",item.Key,item.Value.ToString()); 
                }
    
    0 讨论(0)
  • 2020-12-08 19:14

    If you need a dictionary without values, you might need a HashSet:

    var hashset = new HashSet<string>(stringsArray);
    
    0 讨论(0)
  • 2020-12-08 19:18

    You can use LINQ to do this, but the question that Andrew asks should be answered first (what are your keys and values):

    using System.Linq;
    
    string[] myArray = new[] { "A", "B", "C" };
    myArray.ToDictionary(key => key, value => value);
    

    The result is a dictionary like this:

    A -> A
    B -> B
    C -> C
    
    0 讨论(0)
  • 2020-12-08 19:21

    The Question is not very clear, but Yes you can convert a string to Dictionary provided the string is delimited with some characters to support Dictionary<Key,Value> pair

    So if a string is like a=first;b=second;c=third;d=fourth you can split it first based on ; then on = to create a Dictionary<string,string> the below extension method does the same

    public static Dictionary<string, string> ToDictionary(this string stringData, char propertyDelimiter = ';', char keyValueDelimiter = '=')
    {
        Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
        Array.ForEach<string>(stringData.Split(propertyDelimiter), s =>
            {
                if(s != null && s.Length != 0)
                    keyValuePairs.Add(s.Split(keyValueDelimiter)[0], s.Split(keyValueDelimiter)[1]);
            });
    
        return keyValuePairs;
    }
    

    and can use it like

    var myDictionary = "a=first;b=second;c=third;d=fourth".ToDictionary();
    

    since the default parameter is ; & = for the extension method.

    0 讨论(0)
  • 2020-12-08 19:22

    What do you mean?

    A dictionary is a hash, where keys map to values.

    What are your keys and what are your values?

    foreach(var entry in myStringArray)
        myDictionary.Add(????, entry);
    
    0 讨论(0)
  • 2020-12-08 19:25

    Assuming you're using .NET 3.5, you can turn any sequence (i.e. IEnumerable<T>) into a dictionary:

    var dictionary = sequence.ToDictionary(item => item.Key,
                                           item => item.Value)
    

    where Key and Value are the appropriate properties you want to act as the key and value. You can specify just one projection which is used for the key, if the item itself is the value you want.

    So for example, if you wanted to map the upper case version of each string to the original, you could use:

    var dictionary = strings.ToDictionary(x => x.ToUpper());
    

    In your case, what do you want the keys and values to be?

    If you actually just want a set (which you can check to see if it contains a particular string, for example), you can use:

    var words = new HashSet<string>(listOfStrings);
    
    0 讨论(0)
提交回复
热议问题