现在我有个文件是这个样子的:"Hello!!"
""
"世界!!"
"你现在好吗??"
""
""
""
以上一共七行,我现在想要读取第四行的数据到我指定的string中,并且不要前后的两个"",请提供一个可行的方法,最好是能给我几种不同的读取方法~~~谢谢~~~

解决方案 »

  1.   

    需求略不明。是要指定就读取第四行数据,还是要读取首个字是“你”且全句为你想要的那一行。
    这有区别的。
    StreamReader 的ReadLine可以满足你的需求应该。读取一行后Trim然后判断。
      

  2.   

    ReadLine();i++;
    i是4时,就是读到第四行了
      

  3.   


    是不是这样加入sr是我已读到的文件:
    string link = "";
    for(int i=0;i<4;i++)
    {
      link = sr.ReadLine();
    }
    这样就可以读到第四行的数据了?
      

  4.   


    读第4行的话2楼的方法应该就可以了哈,那假如我事要从“你”开始读取呢?Trim判断应该怎么用?
      

  5.   

    直接根据 \r\n split 一下然后[5] 明白了吧
      

  6.   

    string tempstr = "";
                System.IO.StreamReader sr = new System.IO.StreamReader("123.txt");
               // tempstr = sr.ReadToEnd();
                //string[] rows = tempstr.Split('~');
    string str="";
                //foreach (string str in rows)
           while ((str  = sr.ReadLine()) != null) 
                {
                    //这是普通验证方法
                    if (str.IndexOf("CDC", 0, 3) >= 0)
                    {
                        TextBox.Text+ = str;
                    }
                     //这是正则表达式验证
                    if (System.Text.RegularExpressions.Regex.IsMatch(str, "^CDC", System.Text.RegularExpressions.RegexOptions.IgnoreCase))
                    {
                        TextBox.Text +=str;// System.Text.RegularExpressions.Regex.Match(str, "^CDC", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Value;
                    }
                }
      

  7.   

    不过,使用数组或者直接readline的话也可以!
      

  8.   

    StreamReader objReader = new StreamReader("c:\\test.txt");
        string sLine="";
        string result=string.Empty;
        int i=0;
        while ((sLine = objReader.ReadLine())!= null)
        {
         i++;
         if(i==4)
    {
         result=sLine;
         break;
    }
        }
                             objReader.Close();
      

  9.   

    以上一共七行,我现在想要读取第四行的数据到我指定的string中,并且不要前后的两个""
    ----------------------------------
    string link = "";
    for(int i=0;i<4;i++)
    {
      link = sr.ReadLine();
    }
    link = link.TrimStart('"').TrimEnd('"');
      

  10.   

    简单说下原理:
    直接读第n行对流操作来是不可行的。通常的处理方法是:
    先读出前面n-1行但不处理,直到读出第n行时再处理。至于去掉头尾的引号,截个子串就行了:     class Program
        {
            static void Main(string[] args)
            {
                FileStream fs = null;
                StreamReader r = null;
                string str;
                int row;            try
                {
                    fs = new FileStream("data.txt", FileMode.Open);
                    r = new StreamReader(fs);                do
                    {
                        Console.WriteLine("请输入要查询的行数(>0):");
                        str = Console.ReadLine();                    if (!(int.TryParse(str, out row)))
                            Console.WriteLine("请输入整数!");
                    } while (row < 1);                for (int i = 0; i < row - 1 && !r.EndOfStream; r.ReadLine(), i++) ;                str = r.ReadLine();                Console.WriteLine("查到的行:");                Console.WriteLine(str.Substring(str.IndexOf('"') + 1, str.LastIndexOf('"') - str.IndexOf('"') - 1));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    if (r != null)
                        r.Close();                if (fs != null)
                        fs.Close();
                }
            }
        }