转换工具

Dictionary<string, Dictionary<int, int>> tmpDicts;
void Start () {
    //TestStringToDictionary();

    TestStringToDictionary2();
    Dictionary<int, int> tmp = new Dictionary<int,int>();
    tmp.Add(888888, 0);
    SetKey("40004*0", tmp);
    TestDictionaryToString2();
}

private void TestDictionaryToString2()
{
    StringBuilder sb = new StringBuilder();
    Dictionary<string, Dictionary<int, int>>.Enumerator tmpEnum = tmpDicts.GetEnumerator();

    while (tmpEnum.MoveNext())
    {
        Debug.Log("000key: " + tmpEnum.Current.Key);
        sb.Append(tmpEnum.Current.Key);
        Dictionary<int, int>.Enumerator tmpSkillEnum = tmpEnum.Current.Value.GetEnumerator();
        sb.Append("-");
        while (tmpSkillEnum.MoveNext())
        {
            Debug.Log("111Value: " + tmpSkillEnum.Current.Key + "    " + tmpSkillEnum.Current.Value);
            sb.Append(tmpSkillEnum.Current.Key);
            sb.Append("_");
            sb.Append(tmpSkillEnum.Current.Value);
        }
        sb.Append("#");
    }

    Debug.Log("Dictionary to string: " + sb.ToString().TrimEnd('#'));

}

private void TestStringToDictionary2()
{
    string origionalStr = "40001*0-1001_1#40002*0-2001_1#40003*0-3001_1";
    tmpDicts = ParseStringToDictionary(origionalStr);

    Dictionary<string, Dictionary<int, int>>.Enumerator tmpEnum = tmpDicts.GetEnumerator();

    while (tmpEnum.MoveNext())
    {
        Debug.Log("111key: " + tmpEnum.Current.Key);

        Dictionary<int, int>.Enumerator tmpSkillEnum = tmpEnum.Current.Value.GetEnumerator();

        while (tmpSkillEnum.MoveNext())
        {
            Debug.Log("111Value: " + tmpSkillEnum.Current.Key + "    " + tmpSkillEnum.Current.Value);
        }
    }
    Debug.Log("-----------------------------------------------------------------------------------------------------");
}

public void SetKey(string newKey, Dictionary<int, int> newValue)
{
    if (tmpDicts.ContainsKey(newKey))
    {
        tmpDicts[newKey] = newValue;
        Dictionary<int, int>.Enumerator tmpSkillEnum = tmpDicts[newKey].GetEnumerator();

        while (tmpSkillEnum.MoveNext())
        {
            Debug.Log("222Value: " + tmpSkillEnum.Current.Key + "    " + tmpSkillEnum.Current.Value);
        }
    }
    else
    {
        tmpDicts.Add(newKey, newValue);
    }

    Debug.Log("-----------------------------------------------------------------------------------------------------");

    Dictionary<string, Dictionary<int, int>>.Enumerator tmpEnum = tmpDicts.GetEnumerator();

    while (tmpEnum.MoveNext())
    {
        Debug.Log("333key: " + tmpEnum.Current.Key);

        Dictionary<int, int>.Enumerator tmpSkillEnum = tmpEnum.Current.Value.GetEnumerator();

        while (tmpSkillEnum.MoveNext())
        {
            Debug.Log("333Value: " + tmpSkillEnum.Current.Key + "    " + tmpSkillEnum.Current.Value);
        }
    }
    Debug.Log("-----------------------------------------------------------------------------------------------------");
}

public Dictionary<string, Dictionary<int, int>> ParseStringToDictionary(string origionalStr)
{
    Dictionary<string, Dictionary<int, int>> tmpDict = new Dictionary<string, Dictionary<int, int>>();

    string[] fields = origionalStr.Split('#');

    for (int i = 0; i < fields.Length; i++)
    {
        string[] dictFiled = fields[i].Split('-');
        if (!tmpDict.ContainsKey(dictFiled[0]))
        {
            tmpDict.Add(dictFiled[0], StringToDictionary2(dictFiled[1]));
        }
    }
    return tmpDict;
}

//1001_1
public Dictionary<int, int> StringToDictionary2(string skillStr)
{
    Dictionary<int, int> tmpDict = new Dictionary<int, int>();
    string[] fileds = skillStr.Split('_');
    tmpDict.Add(int.Parse(fileds[0]), int.Parse(fileds[1]));
    return tmpDict;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值