简体   繁体   English

C#文字格式

[英]C# text formatting

What I have is a text file that contains lines in the following format 我所拥有的是一个文本文件,其中包含以下格式的行

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

Format is: Beginning File | 格式为:开始文件| Location | 位置| files ;=multi entry| 文件; =多个条目| total files for all entries 所有条目的文件总数

so for the above text I would need to export to a new file: 因此,对于以上文本,我需要导出到新文件:

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

NOTE : There is pretty much no validation going on here... 注意 :这里几乎没有任何验证...

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM