开发者

C# text formatting

开发者 https://www.devze.com 2022-12-18 12:47 出处:网络
What I have is a text f开发者_运维知识库ile 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

What I have is a text f开发者_运维知识库ile 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;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号