Click here to Skip to main content
14,694,018 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 c#

I have used

while ((line = file.ReadLine()) != null)

               if (line.Contains("FIT"))
                   Console.WriteLine(counter.ToString() + ": " + line);

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

Please help me..

1 solution

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

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

Hope this helps.
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

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