C#文本格式

时间:2010-01-25 17:21:52

标签: c#

我所拥有的文本文件包含以下格式的行

Apple000010095|C:\il_ig\IMPED_IMS\AR001\AR\00\01\|00\{95-99}.TXT; 01\00.TXT; 01\00.TXT|7

格式为: 开始文件|位置| files; =多条目|所有条目的总文件

因此,对于上述文本,我需要导出到一个新文件:

Apple000010095|C:\il_ig\IMPED_IMS\AR001\AR\00\01\|00\{95-99}.TXT|5
Apple000010100|C:\il_ig\IMPED_IMS\AR001\AR\00\01\|01\00.TXT|1
Apple000010101|C:\il_ig\IMPED_IMS\AR001\AR\00\01\|01\01.TXT|1

1 个答案:

答案 0 :(得分:0)

注意:这里几乎没有验证......

class MyObject
{
    string beginningFile;
    string location;
    string[] files;
    int[] count;

    public MyObject(string input)
    {
        string[] barSplit = input.Split(new char[1] { '|' }, StringSplitOptions.RemoveEmptyEntries);

        beginningFile = barSplit[0];
        location = barSplit[1];

        string[] semiSplit = barSplit[2].Split(new char[1] { ';' }, StringSplitOptions.RemoveEmptyEntries);
        List<string> f = new List<string>();
        List<int> c = new List<int>();
        Regex r = new Regex(@"\{(\d+)\-(\d+)\}");

        foreach (string s in semiSplit)
        {
            f.Add(s.Trim());

            if (s.Contains("{"))
            {
                Match m = r.Match(s);
                int x = Convert.ToInt32(m.Groups[2].Value) - Convert.ToInt32(m.Groups[1].Value) + 1;
                c.Add(x);
            }
            else
            {
                c.Add(1);
            }
        }

        files = f.ToArray();
        count = c.ToArray();
    }

    public override string ToString()
    {
        string text = "";

        for(int i = 0; i < files.Length; i++)
        {
            text += string.Format("{0}|{1}|{2}|{3}\n", beginningFile, location, files[i], count[i]);
        }

        return text;
    }
}