在文本文件中查找字符串 [英] Find a string in a text file

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

问题描述

您好,我想知道您是否可以帮助我。我有一个文本文件,我只需要查找一个值,然后将其显示在标签中。

这里是文本片段的示例,它总是在文件的第5行,在标签我只需要显示KALLIE-L



[歌剧]

姓名= KALLIE-L



i得到一个索引超出了数组的范围。 lbloperator.text行上的错误。



i知道我做错了但我不确定如何改变或做对。



  Dim  FileContents() As  < span class =code-keyword> String  = IO.File.ReadAllLines(  C:\ TEST.INI
对于 I = 0 To FileContents.Length - 1

Dim LineParts()作为 字符串 = FileContents(I).Split( Name =

lbloperator.Text = LineParts( 1





任何帮助指向都会有帮助解决方案

试试这个:

 StreamReader file =  null < /跨度>; 
尝试
{
file = new StreamReader( @ C:\ TEST.INI);
while ((line = file.ReadLine())!= null
{
string regMatch = MyString ; // 您的文字搜索
if (Regex.IsMatch(line,regMatch))
{
if (line.Split( Name =)[ 1 ]。ToString()。Trim() !=
lbloperator.Text = line.Split( Name =)[ 1 ]。ToString()。修剪();
}
}
}





--Amit


Hi i wonder if you can help me. i have a text file which i just need to look up one value and then display it in a label.
here is the sample of the text snippet it is always on the 5th line of the file, in the label i only need to display "KALLIE-L"

[Operater]
Name=KALLIE-L

i get an Index was outside the bounds of the array. error on lbloperator.text line.

i know i am doing it wrong but i am not sure how to change or get it right.

Dim FileContents() As String = IO.File.ReadAllLines("C:\TEST.INI")
        For I = 0 To FileContents.Length - 1

            Dim LineParts() As String = FileContents(I).Split("Name=")

            lbloperator.Text = LineParts(1)



any assistance pointing will be helpfull

解决方案

Try this:

StreamReader file = null;
try
{
    file = new StreamReader(@"C:\TEST.INI");
    while ((line = file.ReadLine()) !=null)
    {
        string regMatch = "MyString"; //Your Text To search
        if(Regex.IsMatch (line, regMatch))
        {
            if(line.Split("Name=")[1].ToString().Trim() != "")
                lbloperator.Text = line.Split("Name=")[1].ToString().Trim();
        }
    }
}



--Amit


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

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