my code here tries to find a character 'Y'or 'y' at the 15th position of the string, if found i need to replace the 15th position with a space. Here's the content of my text file -

the flower is yyellow in color
the flower is yyellow in color
the flower is yyellow in color
the flower is yyellow in color

...and here's my code.

        var txtFiles = Directory.GetFiles(@"E:\PROJ\replaceY\replaceY\", "*.txt");
        try
        {
            foreach (string currentFile in txtFiles)
            {
                var append = false;
                string[] lines = File.ReadAllLines(currentFile);
                foreach (string line in lines)
                {
                    var theString = line;
                    var aStringBuilder = new StringBuilder(theString);

                    aStringBuilder.Insert(15, "  ");
                    theString = aStringBuilder.ToString();
                    using (StreamWriter outfile = new StreamWriter(currentFile, append))
                    {
                        outfile.Write(theString.ToString() + Environment.NewLine);
                    }
                    append = true;

                    Console.WriteLine(theString);
                    Console.ReadKey();
                }
            }
        }
        catch (IOException ex)
        {
            Console.WriteLine(ex.Message);
        }

Related posts

Recent Viewed