0

I have a string of value "13,104,76,73,47,94" what I would like to do is convert this to a dictionary so that 13 is a key and 104 is the value, 76 is the key and 73 is the value..and so on. I would show you some sample code but honestly have have no idea at all how i can do this so there really is none to show.

Thanks

sean
  • 47
  • 1
  • 7
  • Call `Split()` then write a loop. – SLaks Mar 15 '15 at 12:18
  • ... or [this](http://stackoverflow.com/questions/28081011/c-sharp-split-namevaluepair-string-with-delimitter-in-values-using-regex) or [this](http://stackoverflow.com/questions/2049048/c-sharp-extract-values-from-key-value-pairs-in-string) or [this](http://stackoverflow.com/questions/26700562/c-sharp-regex-to-extract-key-value) or [this](http://stackoverflow.com/questions/11673731/parse-a-string-with-name-value-pairs) or [this](http://stackoverflow.com/questions/24136021/how-to-extract-key-value-pairs-from-a-string-when-the-key-itself-is-the-separat) ... – O. R. Mapper Mar 15 '15 at 12:20
  • ... or [this](http://stackoverflow.com/questions/4141208/convert-a-delimted-string-to-a-dictionarystring-string-in-c-sharp) - this has also been discussed on [MSDN forums](https://social.msdn.microsoft.com/Forums/vstudio/en-US/793177b0-f75c-4a12-a02d-695382caf2b8/splitting-a-string-to-key-value-pair?forum=csharpgeneral). I found all these by simply googling for *c# split string key value*. – O. R. Mapper Mar 15 '15 at 12:21
  • oh well dayum didn't think about googling those keywords, i googled `convert array to dictionary key and value` – sean Mar 15 '15 at 12:24
  • 1
    @sean: You need to think about what steps are involved in doing that. – SLaks Mar 15 '15 at 12:27

2 Answers2

0

Basically, this is very simple.

Dictionary<int, string> dictionary = new Dictionary<int, string>(); for (int i = 0; i < numOfStringsYouHave; i+=2) { dictionary.Add(StringArray[i].ToInt32, StringArray[i+1]); }

Converting String

Dictionary docs C#

Snowman
  • 1,503
  • 1
  • 17
  • 39
  • 1
    Actually, [`Add`](https://msdn.microsoft.com/en-us/library/k7z0zy8k%28v=vs.110%29.aspx) will *not* overwrite previously existing values, it will throw an [`ArgumentException`](https://msdn.microsoft.com/en-us/library/system.argumentexception%28v=vs.110%29.aspx) if the key already exists. – O. R. Mapper Mar 15 '15 at 12:26
  • 1
    Also, the statement "probably what you will want is a hashtable" doesn't make much sense - [`Dictionary`](https://msdn.microsoft.com/en-us/library/xfhwa508%28v=vs.110%29.aspx) *is* a hashtable (cf. the docs you linked to: "the `Dictionary` class is implemented as a hash table."). What needs to be done to get multiple values per key is using a collection type as the value, though I think there are already plenty of questions on SO that disucss that. – O. R. Mapper Mar 15 '15 at 12:26
  • forgot to mention the dictionary will be cleared before this function is called so there's no worries about overwriting anything – sean Mar 15 '15 at 12:38
  • @sean: The overwriting (or rather, exception) happens if there are duplicate keys within your string, regardless of whether previous contents of the dictionary are cleared. – O. R. Mapper Mar 15 '15 at 12:59
0

Just pseudo-code, i didn't compile it, but it should work fine and give you an idea:

// Your data
string values = "1,10,2,20,3,30";
// Split string by comma
string[] splitted = values.split(',');

// declare a new dictionary
Dictionary<int,int> dict = new Dictionary<int,int>();

// for loop on the splitted data, the counter is increased by 2.
for(int i;i<splitted.Length,i+=2)
{
    //Parse the string data as integer and add it to the dictionary.
    dict.Add(int.Parse(splitted[i]), int.Parse(splitted[i+1]))
}
devmb
  • 805
  • 6
  • 18