Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to search for a string from a text file..in c#

I have used

C#
while ((line = file.ReadLine()) != null)
           {

               if (line.Contains("FIT"))
               {
                   Console.WriteLine(counter.ToString() + ": " + line);
               }
                   counter++;
 file.Close();
}


my text file contains "FIT" and "UNFIT"
the above code gives me both the values. but I need "FIT" only

Please help me..
Posted

1 solution

You can use a regular expression:
C#
using System.Text.RegularExpressions; // add this at the top of your code file

C#
if (Regex.IsMatch(line, @"\bFIT\b"))
{
    Console.WriteLine(counter.ToSring() + ": " + line);
}

Hope this helps.
 
Share this answer
 
Comments
BillWoodruff 26-Oct-13 12:22pm    
Vote => 5 Nice !
Thomas Daniels 26-Oct-13 12:23pm    
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900