Advertisement
ScorpS

Replace Start With Finish

Jan 26th, 2013
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.16 KB | None | 0 0
  1.  
  2. //Write a program that replaces all occurrences of the substring "start" with the substring "finish"
  3. //in a text file. Ensure it will work with large files (e.g. 100 MB).
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.IO;
  11.  
  12. class ReplaceStartWithFinish7
  13. {
  14.     static void Main()
  15.     {
  16.         string start = "start";
  17.         string finish = "finish";
  18.         StreamReader read = new StreamReader("../../input.txt");
  19.         StreamWriter write = new StreamWriter("../../input2.txt");
  20.         string line = "";
  21.  
  22.         using (read)
  23.         {
  24.             using (write)
  25.             {
  26.                 line = read.ReadLine().ToLower();
  27.                 while (line != null)
  28.                 {
  29.                     line = line.Replace(start, finish);
  30.                     write.WriteLine(line);
  31.                     line = read.ReadLine();
  32.                 }
  33.             }
  34.         }
  35.         File.Replace("../../input2.txt", "../../input.txt", "../../backup.txt");
  36.         // if you want to delete backup file uncomment next row
  37.         //File.Delete("../../backup.txt");
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement