文本文件中的完全匹配字符串 [英] exact/match string in textfile

查看:99
本文介绍了文本文件中的完全匹配字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在c#中的文本文件中找到精确/匹配的字符串?

How can I find exact/match string in textfile in c#?

推荐答案

您可以读取文件并匹配.txt文件数据中的字符串
这是一个简单的例子

you can read file and match the string in the data of .txt file
here is simple example

private int GetLinesNoFromFile(string FileNameWithPath,string searchString)
        {
            string[] bt = null;
            if (System.IO.File.Exists(FileNameWithPath))
            {
                bt = System.IO.File.ReadAllLines(FileNameWithPath);
            }

            for (int i = 0; i < bt.Length; i++)
            {
                string str = bt[i].ToString();
                if (str.Contains(searchString))
                    return i; // data found in line no
            }
            return -1; // nodata found
        }


有很多方法可以实现这一目标.一切都从读入文件开始.读入文件后,可以使用许多字符串方法之一来标识文本,也可以使用正则表达式.合适的技术实际上取决于您以后将如何处理此信息(例如,如果您需要匹配的位置,则正则表达式不是最佳选择).
There are so many ways to achieve this. All start with reading the file in. Once you''ve read the file in, you could use one of the many string methods to identify the text, or you could use a regular expression. The appropriate technique really depends on what you are going to do with this information afterwards (e.g. if you need the position of the match, then a regular expression isn''t the best choice).


莫森(Mohsen)试试这个


私有布尔型FindMyString(字符串thisFileName,字符串lookingFor,输出int foundIt,输出字符串errorMsg)
{
字符串entry = string.Empty;
int fileIndex;

foundIt = 0;
errorMsg = string.Empty;
如果(string.IsNullOrEmpty(thisFileName))
{
errorMsg =未传递任何文件.";
返回false;
}
如果(!File.Exists(thisFileName))
{
errorMsg =文件名" + thisFileName +不存在?";
返回false;
}
TextReader myTR = new StreamReader(thisFileName);
fileIndex = 0;
while((entry = myTR.ReadLine())!= null)
{
如果(entry.ToUpper().Trim().包含(lookingFor))
{
foundIt = fileIndex + 1;
myTR.Close();
返回true;
}
}
在"+ thisFileName +".中找不到errorMsg = lookingFor +;
myTR.Close();
返回false;

}


主要功能示例:


int foundIt = 0;
字符串errorMsg = string.Empty;
字符串lookingFor =我的朋友";
字符串thisFileName ="c:\\ Test.txt";


如果(FindMyString(thisFileName,lookingFor,out foundIt,out errorMsg)== false)
Console.WriteLine(找不到您的+ +寻找+".);
其他
Console.WriteLine("Your" + lookingFor +在点" + foundIt +."处找到);
Mohsen try this


private Boolean FindMyString(string thisFileName, string lookingFor, out int foundIt, out string errorMsg)
{
string entry=string.Empty ;
int fileIndex;

foundIt = 0;
errorMsg = string.Empty;
if (string.IsNullOrEmpty(thisFileName))
{
errorMsg = "No File passed on.";
return false;
}
if (!File.Exists(thisFileName))
{
errorMsg = "File name " + thisFileName + " does not exist?";
return false;
}
TextReader myTR = new StreamReader(thisFileName);
fileIndex = 0;
while ((entry = myTR.ReadLine()) != null)
{
if (entry.ToUpper().Trim().Contains(lookingFor))
{
foundIt = fileIndex+1;
myTR.Close();
return true;
}
}
errorMsg = lookingFor + " was not found in " + thisFileName + ".";
myTR.Close();
return false;

}


Example in your main function:


int foundIt=0;
string errorMsg=string.Empty;
string lookingFor="My FRIEND";
string thisFileName="c:\\Test.txt";


if (FindMyString(thisFileName, lookingFor, out foundIt, out errorMsg) == false)
Console.WriteLine("Your " + lookingFor + " was not found.");
else
Console.WriteLine("Your " + lookingFor + " was found at the point " + foundIt + ".");


这篇关于文本文件中的完全匹配字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆