locked
(c#) Every third line of the Text file from lowercase to uppercase RRS feed

  • Question

  • Hello everyone,
    This code below is used to convert Text files from lowercase to uppercase ...

    I would like the code to convert every third line of Text file (starting from the first line), from lowercase to uppercase ...?

    I ask for your help. Thanks in advance.

     private void Touppercase()
            {
                try
                {
                    if (File.Exists(textBox1.Text)) // Path to the Txt file
                    {
                        StreamReader fileRw = File.OpenText(textBox1.Text); // Path to the Txt file
                        
                        StreamWriter fileWr = File.CreateText(textBox2.Text + ".txt"); // Path to a "new" Text file with upper case
                        string line;
                        do
                        {
                            line = fileRw.ReadLine();
                            if (line != null)
                                fileWr.WriteLine(line.ToUpper());
                        }
                        while (line != null);
                        fileRw.Close();
                        fileWr.Close();
                    }
                }
                catch
                {
    
                }
            }


    Wednesday, November 25, 2020 3:45 PM

Answers

  • Hi denysH,

    Thank you for posting here.

    I make a test on my side, and you can refer to the following code.

            private void Touppercase()
            {
                if (File.Exists(textBox1.Text)) // Path to the Txt file
                {
                    int index = 0;
                    string[] lines = File.ReadAllLines(textBox1.Text);
    
                    while (index < lines.Length)
                    {
                        if ((index + 1) % 3 == 0)
                        {
                            lines[index] = lines[index].ToUpper();   
                        }
                        index++;
                    }
                    File.WriteAllLines(textBox2.Text + ".txt", lines);
                }
            }

    Content in file1.

    Result of the test.

    Hope it could be helpful.

    Best Regards,

    Xingyu Zhao

     

    Visual Basic and CLR forum will be migrating to a new home on Microsoft Q&A! (VB.NET and CLR) We invite you to post new questions in the new home on Microsoft Q&A ! For more information, please refer to the sticky post(VB.NET and CLR).

    • Marked as answer by denysH Thursday, November 26, 2020 8:27 AM
    Thursday, November 26, 2020 3:03 AM

All replies

  • Hi denysH,

    Thank you for posting here.

    I make a test on my side, and you can refer to the following code.

            private void Touppercase()
            {
                if (File.Exists(textBox1.Text)) // Path to the Txt file
                {
                    int index = 0;
                    string[] lines = File.ReadAllLines(textBox1.Text);
    
                    while (index < lines.Length)
                    {
                        if ((index + 1) % 3 == 0)
                        {
                            lines[index] = lines[index].ToUpper();   
                        }
                        index++;
                    }
                    File.WriteAllLines(textBox2.Text + ".txt", lines);
                }
            }

    Content in file1.

    Result of the test.

    Hope it could be helpful.

    Best Regards,

    Xingyu Zhao

     

    Visual Basic and CLR forum will be migrating to a new home on Microsoft Q&A! (VB.NET and CLR) We invite you to post new questions in the new home on Microsoft Q&A ! For more information, please refer to the sticky post(VB.NET and CLR).

    • Marked as answer by denysH Thursday, November 26, 2020 8:27 AM
    Thursday, November 26, 2020 3:03 AM
  • It does the job, that's it ...
    Thank you very much Xingyu Zhao.
    Thursday, November 26, 2020 8:35 AM