Hello everybody!
I want to find a string in the txt file. Example I have a file .File Name is test.txt and data in this file has "Hello Everybody . I am Toan" . And I enter a string "I am" ,the result will return "true" because the file constains the string. I am using C#. Can you help me to do it? Thanks very much
Find string in txt file
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.

Satyapriya NayakRank #47 · 31.5K reputationPosted Apr 6, 2012, 7:02 PM EDT
Try this...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
namespace search
{
class Program
{
static void Main(string[] args)
{
string fName = @"D:\test.txt";
StreamReader testTxt = new StreamReader(fName);
string allRead = testTxt.ReadToEnd();
testTxt.Close();
string regMatch = "I am";
if (Regex.IsMatch(allRead,regMatch))
{
Console.WriteLine("True:Found\n");
}
else
{
Console.WriteLine("False:Not found\n");
}
Console.ReadLine();
}
}
}
Thanks
If this post helps you mark it as answer
toanhoi bui70 reputationPosted Apr 6, 2012, 7:34 PM EDT
Sam HobbsRank #107 · 20.5K reputationPosted Apr 7, 2012, 4:52 PM EDT