How to remove text in brackets from all the strings in a text file with C#

2 Answers

0 votes
using System.IO;
using System.Text.RegularExpressions;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamWriter sw = new StreamWriter("d:\\new_data.txt");

            string line;

            StreamReader sr = new StreamReader("d:\\data.txt");
            while ((line = sr.ReadLine()) != null)
            {
                line = Regex.Replace(line, @" ?\(.*?\)", string.Empty);
                sw.WriteLine(line);
            }

            sr.Close();
            sw.Close();
        }
    }
}


/*
run:
    
c# vb.net

*/

 



answered Sep 15, 2017 by avibootz
0 votes
using System;
using System.IO;
using System.Text.RegularExpressions;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            string text = "";

            using (StreamReader sr = new StreamReader("d:\\data"))
            {
                do
                {
                    string line = sr.ReadLine();
                    text += Regex.Replace(line, @" ?\(.*?\)",string.Empty) + Environment.NewLine;

                } while (sr.EndOfStream == false);
            }
            File.WriteAllText("d:\\data", text);
        }
    }
}


/*
run:


*/

 



answered Sep 15, 2017 by avibootz

Related questions

1 answer 263 views
1 answer 168 views
1 answer 192 views
1 answer 174 views
1 answer 214 views
...