0

In my program i have a piece of code which add characters in a dictionary

 var listOfSimilarCharacters = new Dictionary<string, string>();
 listOfSimilarCharacters.Add("l", "!");
 listOfSimilarCharacters.Add("1", "i");
 listOfSimilarCharacters.Add("O", "0");
 listOfSimilarCharacters.Add("o", "I");

But above in my code i have a property "SimilarCharacters" i need to reed this out and delete the four listOfSimilarCharacters.Add lines

/// <summary>
/// A constant that is lists all similar characters.
/// </summary>
public const string SimilarCharacters = "l!1iO0oI";

in some way i need to add the chars from SimilarCharacters in a dictionary and read the SimilarCharacters out so i can use this instead of the four lines.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • http://stackoverflow.com/questions/7306767/linq-to-convert-a-string-to-a-dictionarystring-string – Alex Mar 06 '13 at 10:03

2 Answers2

0

You can use collection initializer to fill your dictionary:

var listOfSimilarCharacters = new Dictionary<string, string> { 
  { "l", "!" }, { "1", "i" }, {"O", "0"}, {"o", "I"} 
};

BTW why are you using strings to store chars?

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
0
        string SimilarCharacters = "l!1iO0oI";

        var listOfSimilarCharacters = new Dictionary<string, string>();

        bool timeToAdd = false;
        string key = String.Empty;
        string value = String.Empty;
        foreach ( var c in SimilarCharacters )
        {
            if ( timeToAdd )
            {
                value = c.ToString();
                listOfSimilarCharacters.Add( key, value );
            }
            else
            {
                key = c.ToString();
            }
            timeToAdd = !timeToAdd;
        }
user1735930
  • 99
  • 1
  • 1
  • 4