C# - 格式化.text文档问题

时间:2011-07-15 21:40:36

标签: c# list format text-files spacing

这是我正在加载的文本文件:

原始文件

10 BARE PCB
20 T C40
B C5, C45, C48
30 B C25
40 B C17, C18
50 B C15
60 T C20, C23,
B C6, C7, C8, C9, C10, C11, C12,
C31, C32, C33, C34, C35, C36,
C37, C38, C39
70 T C16, C21, C22, C24, C41, C42
B C3, C19, C43, C44, C47
80 T C13, C14
B C1, C2
90 B C26, C27, C28, C29, C30
100 T R65
110 T R35
120 T R34
130 T R33
140 T R21, R22, R29, R30
150 B R28, R31, R32, R37
160 T R17, R19, R26, R47, R50, R51,
R53, R57, R58, R59
B R18, R25, R42, R48, R49, R52,
R54, R55, R56, R60
170 T R23
B R10, R43
180 T R8, R9
190 T R13, R14, R15, R61, R62, R63,
R64
200 T R27, R38, R39, R40, R41
B R2, R3, R11, R44, R45, R46
210 B R1
220 T C4
230 T D1
240 T U1
250 B U10
270 B U6
280 B U5
290 B U4
300 T U2
310 B U7
320 B U8
330 T U9
340 B L2, L3
350 B L8, L9
360 B L1, L4, L5, L6, L7, L10
370 B J1, J2
380 B J3
390 B X1
400 T X2
410 B J4
420 B J5
422 B U3
2000 T TRACKING LABEL
423 ADHESIVE
424 ACCELERATOR
425 ADHESIVE

这就是我将文件格式化为:

格式化文件

0010 BARE PCB
0020 C40
0020A C5, C45, C48
0030A C25
0040A C17, C18
0050A C15
0060 C20, C23,
0060A C6, C7, C8, C9, C10, C11, C12,C31, C32, C33, C34, C35, C36,C37, C38, C39
0070 C16, C21, C22, C24, C41, C42
0070A C3, C19, C43, C44, C47
0080 C13, C14
0080A C1, C2
0090A C26, C27, C28, C29, C30
0100 R65
0110 R35
0120 R34
0130 R33
0140 R21, R22, R29, R30
0150A R28, R31, R32, R37
0160 R17, R19, R26, R47, R50, R51,R53, R57, R58, R59
0160A R18, R25, R42, R48, R49, R52,R54, R55, R56, R60
0170 R23
0170A R10, R43
0180 R8, R9
0190 R13, R14, R15, R61, R62, R63,R64
0200 R27, R38, R39, R40, R41
0200A R2, R3, R11, R44, R45, R46
0210A R1
0220 C4
0230 D1
0240 U1
0250A U10
0270A U6
0280A U5
0290A U4
0300 U2
0310A U7
0320A U8
0330 U9
0340A L2, L3
0350A L8, L9
0360A L1, L4, L5, L6, L7, L10
0370A J1, J2
0380A J3
0390A X1
0400 X2
0410A J4
0420A J5
0422A U3
2000 TRACKING LABEL
0423 ADHESIVE
0424 ACCELERATOR
0425 ADHESIVE

无论其

如果你仔细观察格式化的文件中有一些点,其中一个逗号后面没有空格(C12,C31& C36,C37& R51,R53& R52,R54& R63,R64 )。我希望那里有一个空间..

CODE

    private void openRefsFormatHelper()
    {
        try
        {
            // Resets the formatted refs text.
            formattedRefsTextRichTextBox.ResetText();

            // Reads the lines in the file to format.
            var reader = File.OpenText(openRefs.FileName);

            // Creates a list for the lines to be stored in.
            var list = new List<string>();

            // Adds each line in the file to the list.
            while (true)
            {
                var line = reader.ReadLine();
                if (line == null)
                    break;
                list.Add(line);
            }

            // Handles all of the requirements for the reference text.
            list = fourDigitRequirement(list);
            list = concatRequirement(list);
            list = startsWithBRequirement(list);
            list = elementIsBRequirement(list);
            list = removeTRequirement(list);

            // Prints the formatted refs to the richtextbox.
            foreach (var line in list)
                formattedRefsTextRichTextBox.AppendText(line + "\n");
        }

        // Catches an exception if the file could not be formatted.
        catch (Exception)
        {
            MessageBox.Show("There was a problem formatting the 'Refs File'.", "Refs File Format Error",
                MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }

    static List<string> elementIsBRequirement(List<string> list)
    {
        // Creates a new list to return with new format.
        var result = new List<string>();

        // Checks each line in the list.
        foreach (var line in list)
        {
            // Splits each line into 'parts'
            var parts = line.Split(' ');

            // Checks if the second 'part' array is "B"
            if (parts[1].Equals("B"))
            {
                // If it is "B", replace with "A" and add to the new list "result"
                parts[0] += "A";
                parts[1] = string.Empty;

                result.Add(string.Join(" ", parts));
            }

            // Otherwise keep the line how it is.
            else
                result.Add(line);
        }

        // Returns the new list so it can be formatted further.
        return result;
    }

    static List<string> startsWithBRequirement(List<string> list)
    {
        // Creates a new list to return with new format.
        var result = new List<string>();
        var i = 0;

        // Checks if the line begins with "B"
        foreach (var line in list)
        {
            // Splits each line into 'parts'
            var parts = line.Split(' ');

            // Checks if the first 'part' array is "B"
            if (parts[0].Equals("B"))
            {
                // If it is "B", copy the previous line down and add "A" where "B" was at
                // and add to the new list "result".
                parts[0] = string.Empty;
                result.Add(list[i - 1].Split(' ')[0] + "A" + string.Join(" ", parts));
            }

            // Otherwise keep the line how it is.
            else
                result.Add(line);

            i++;
        }

        // Returns the new list so it can be formatted further.
        return result;
    }

    static List<string> concatRequirement(List<string> list)
    {
        // Creates a new list to return with new format.
        var result = new List<string>();

        // Checks each line in the list.
        foreach (var line in list)
        {
            // Splits each line into 'parts'
            var parts = line.Split(' ');
            int test;

            // Concats everything together
            if (int.TryParse(parts[0], out test) || parts[0].Equals("B"))
                result.Add(line);

            // Otherwise result - 1
            else
                result[result.Count - 1] += line;
        }

        // Returns the new list so it can be formatted further.
        return result;
    }

    static List<string> removeTRequirement(List<string> list)
    {
        // Creates a new list to return with new format.
        var result = new List<string>();

        // Checks each line in the list.
        foreach (var line in list)
        {
            // Splits each line into 'parts'
            var parts = line.Split(' ');

            // Checks if the second 'part' array is "T", if it is, remove "T"
            if (parts[1].Equals("T"))
                parts[1] = string.Empty;

            // Add the new string to the result.
            result.Add(string.Join(" ", parts).Replace("  ", " "));
        }

        // Returns the new list so it can be formatted further.
        return result;
    }

    static List<string> fourDigitRequirement(List<string> list)
    {
        // Creates a new list to return with new format.
        var result = new List<string>();

        // Checks each line in the list.
        foreach (var line in list)
        {
            // Splits each line into 'parts'
            var parts = line.Split(' ');
            int test;

            // Checks if the array[0] (digits) is the proper length.
            if (int.TryParse(parts[0], out test))
            {
                // If it is not a length of 4 digits, add "O" to the front until it is.
                parts[0] = parts[0].PadLeft(4, '0');

                // Add the new string to the result list.
                result.Add(string.Join(" ", parts));
            }

            // Otherwise keep the line how it is.
            else
                result.Add(line);
        }

        // Returns the new list so it can be formatted further.
        return result;
    }

问题: 的   - 我如何在那里获得那个空间?

2 个答案:

答案 0 :(得分:2)

没有桌面检查整个事情,我注意到问题条目是值在行尾的位置。

尝试在分割之前向行添加空格。也许

答案 1 :(得分:2)

在concatRequirement中,您可以更改:

            result[result.Count - 1] += line;

            result[result.Count - 1] += " " + line;