w3resource logo


C# Sharp Programming Exercises

C# Sharp Exercises: Create and write some line of text which does not contain a given string in a line

Secondary Nav

C# Sharp File Handling : Exercise-7 with Solution

Write a program in C# Sharp to create and write some line of text into a file which does not contain a given string in a line.
Test Data :
Input the string to ignore the line : fox
Input number of lines to write in the file : 2
Input 2 strings below :
Input line 1 : the quick brown fox jumps
Input line 2 : over the lazy dog.
Expected Output :

The line has ignored which contain the string 'fox'.                                                        
 The content of the file is  :                                                                                
----------------------------------                                                                            
 over the lazy dog.                                                      

C# Sharp Code:

using System;
using System.IO;


class WriteTextFile
{
    static void Main()
    {
		string fileName = @"mytest.txt"; 
		string[] ArrLines;
		string str;
		
		int n,i;

	    Console.Write("\n\n Create and write some line of text which does not contain a given string in a line  :\n");
		Console.Write("------------------------------------------------------------------------------------------\n"); 

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
        Console.Write(" Input the string to ignore the line : ");
        str = Console.ReadLine();  
       Console.Write(" Input number of lines to write in the file  : ");
       n= Convert.ToInt32(Console.ReadLine()); 
       ArrLines=new string[n];  
       
       Console.Write(" Input {0} strings below :\n",n);
		for(i=0;i<n;i++)
		{
		Console.Write(" Input line {0} : ",i+1);
		ArrLines[i] = Console.ReadLine();	
		}
	
        using (System.IO.StreamWriter file = 
            new System.IO.StreamWriter(@"mytest.txt"))
        {
            foreach (string line in ArrLines)
            {
                if (!line.Contains(str)) // write the line to the file If it doesn't contain the string in str
                {
                    file.WriteLine(line);
                }
            }
        }		
        
	using (StreamReader sr = File.OpenText(fileName)) 
        {
            string s = "";
            Console.Write("\n The line has ignored which contain the string '{0}'. \n",str);
            Console.Write("\n The content of the file is  :\n",n);
            Console.Write("----------------------------------\n");
            while ((s = sr.ReadLine()) != null) 
            {
                Console.WriteLine(" {0} ",s);
            }
            Console.WriteLine();
        }        
    }
}

C# Sharp Practice online:


using System;
using System.IO;


class WriteTextFile
{
    static void Main()
    {
		string fileName = @"mytest.txt"; 
		string[] ArrLines;
		string str;
		
		int n,i;

	    Console.Write("\n\n Create and write some line of text which does not contain a given string in a line  :\n");
		Console.Write("------------------------------------------------------------------------------------------\n"); 

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
        Console.Write(" Input the string to ignore the line : ");
        str = Console.ReadLine();  
       Console.Write(" Input number of lines to write in the file  : ");
       n= Convert.ToInt32(Console.ReadLine()); 
       ArrLines=new string[n];  
       
       Console.Write(" Input {0} strings below :\n",n);
		for(i=0;i<n;i++)
		{
		Console.Write(" Input line {0} : ",i+1);
		ArrLines[i] = Console.ReadLine();	
		}
	
        using (System.IO.StreamWriter file = 
            new System.IO.StreamWriter(@"mytest.txt"))
        {
            foreach (string line in ArrLines)
            {
                if (!line.Contains(str)) // write the line to the file If it doesn't contain the string in str
                {
                    file.WriteLine(line);
                }
            }
        }		
        
	using (StreamReader sr = File.OpenText(fileName)) 
        {
            string s = "";
            Console.Write("\n The line has ignored which contain the string '{0}'. \n",str);
            Console.Write("\n The content of the file is  :\n",n);
            Console.Write("----------------------------------\n");
            while ((s = sr.ReadLine()) != null) 
            {
                Console.WriteLine(" {0} ",s);
            }
            Console.WriteLine();
        }        
    }
}

Improve this sample solution and post your code through Disqus.



Join our Question Answer community to learn and share your programming knowledge.