0

在我的程序中,我有一段代码在字典中添加字符

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

但是在我的代码上面我有一个属性“SimilarCharacters”我需要重新删除它并删除listOfSimilarCharacters.Add四行

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

在某种程度上,我需要从SimilarCharacters字典中添加字符并读SimilarCharacters出,这样我就可以使用它而不是四行。

4

2 回答 2

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;
        }
于 2013-03-06T10:06:40.813 回答
0

您可以使用集合初始化程序来填充您的字典:

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

顺便说一句,你为什么使用字符串来存储字符?

于 2013-03-06T10:04:23.347 回答