解析字符串并拆分它的最安全的方法

时间:2014-09-15 14:25:17

标签: c#

我有一个类似的字符串:

UNIQUE_ID-String-TriggerID
82-DEFAULT-4;MINIMODE_ARCADE-1;MINIMODE_RUNNER-4;47-STARLING-1; 

一些输入示例:

1) 82-Mode1-4;
2) 4-Arcade-2;9-Runner-2;90-STARLING-1;  // here you see Unique ID 4 has two entries 
3) 82-DEFAULT-4;MINIMODE_ARCADE-1;MINIMODE_RUNNER-4;47-STARLING-1; 2-DEFAULT-4;MINIMODE_ARCADE-0; 
// here 82 UNIQUE ID and 2 UNIQUE ID has two enteritis, but 47 UNIQUE ID has just one enterey

问题是有时候我只得到一个UniqueID 47的条目,有时候我会得到ID 82的多个条目。

如何正确拆分和解析字符串?

我将解析后的字符串放在字典中,所以有时我会得到相同的密钥来添加并引发异常

我已完成以下操作,但仍无法追踪所有情况:

        String data = Encoding.UTF8.GetString(Convert.FromBase64String(Progress));

        String[] progressEntries = data.Split(';');

        for (int i = 0; i < progressEntries.Length; i++)
        {
            String entry = progressEntries[i];
            String[] values = entry.Split('-');

            if (values.Length > 2)
            {
                Dictionary<string, int> dict = new Dictionary<string, int>();
                dict[values[1]] = Convert.ToInt32(values[2]);
                Progress.Add(Convert.ToInt32(values[0]), dict);
            }
        }
    } 

1 个答案:

答案 0 :(得分:2)

这不太清楚,但让我们试一试。 在您提供的文字中:

82-DEFAULT-4;MINIMODE_ARCADE-1;MINIMODE_RUNNER-4;47-STARLING-1; 

你有两组。每个组都有键值对

  • 第1组 - 关键:82
    • 默认:4
    • MINIMODE_ARCADE:1
    • MINIMODE_RUNNER:4
  • 第2组 - 关键:47
    • STARLING:1

如果这是您想要获得的结构,那么一种存储可能性为Dictionary<int,Dictionary<string,int>>

我认为以下代码可以:

public class Parser
{
    public Dictionary<int, Dictionary<string, int>> Parse(string input)
    {
        Dictionary<int, Dictionary<string, int>> data = new Dictionary<int, Dictionary<string, int>>();

        int? currentGroupKey = null;
        string[] keyValuePairs = input.Split(new char[] { ';' });
        foreach (var kvp in keyValuePairs)
        {
            string[] tokens = kvp.Split(new char[] { '-' });
            switch (tokens.Length)
            {
                case 2:
                    {
                        if (currentGroupKey.HasValue)
                        {
                            int groupKey = currentGroupKey.Value;
                            AddKeyValuePair(data, groupKey, tokens[0], tokens[1]);
                        }
                        break;
                    }
                case 3:
                    {
                        int groupKey;
                        if (int.TryParse(tokens[0], out groupKey))
                        {
                            currentGroupKey = groupKey;
                            AddKeyValuePair(data, groupKey, tokens[1], tokens[2]);
                        }
                        break;
                    }
                default:
                    break;
            }

        }
        return data;
    }

    private void AddKeyValuePair(Dictionary<int, Dictionary<string, int>> data, int groupKey, string key, string val)
    {
        Dictionary<string, int> group;
        if (data.ContainsKey(groupKey))
        {
            group = data[groupKey];
        }
        else
        {
            group = new Dictionary<string, int>();
            data[groupKey] = group;
        }

        int intVal;
        if (int.TryParse(val, out intVal))
            group.Add(key, intVal);
    }
}