HI 高手们:我现在有个文档内容是:
COM=COM3
Baudrate=19200
Parity=Even
DataBits=7
StopBits=One目的:
我要读取这里的每一行,获取每一行等于号右边的内容放入一个string数组现在代码:private string text()
{
    string[] comAry = new string[4]{};
    string Path = "text.txt";
    FileStream fs = new FileStream(Path , Path.Open);
    StreamReader sr = new StreamReader(fs);
    line = sr.ReadLine();
    while (line.IndexOf("=") > -1)
    {
         comAry[i] = line.Substring(line.IndexOf("="), line.Length - line.IndexOf("="));
         i++;
         line = sr.ReadLine();
    }
}问题:
我现在获取到的内容会把等于号一起加进去,请问有什么办法解决

解决方案 »

  1.   

    comAry[i] = line.Substring(line.IndexOf("=")+1, line.Length - line.IndexOf("="));
      

  2.   


    using(StreamReader reader=new StreamReader(@"E:\Test.txt"))
    {
       string line=string.Empty;
       List<string> result=new List<string>();
       
       while(line!=null)
       {
          line=reader.ReadLine();
          
          if(string.IsNullOrEmpty(line))
          {
             continue;
          }      if(!line.Contains("="))
          {
             continue;
          }      result.Add(line.Spilt('=')[1]);
       }
    }
      

  3.   


     string[] comAry = null;                comAry = File.ReadAllLines(@"C:\Documents and Settings\Administrator\桌面\Test.txt", Encoding.GetEncoding("GB2312")).Where(a => a.IndexOf("=") > 0).Select(a => { return Regex.Match(a, @"(?<==\s*)[^\s]+").Value; }).ToArray();
                    /*
                      [0] "COM3" string
                    [1] "19200" string
                    [2] "Even" string
                    [3] "7" string
                    [4] "One" string                 */
      

  4.   

    1.截取字符串往往不是最好的办法2.文件流操作时一定要时刻记住释放的问题,所以最好的方式就是使用using语句块
      

  5.   

            private void text()
            {
                List<string> comAry = new List<string>();
                int i = 0;
                string Path = "text.txt";
                FileStream fs = new FileStream(Path,FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                string line = sr.ReadLine();
                while (line!= null && line.IndexOf("=") > -1)
                {
                    comAry.Add(line.Substring(line.IndexOf("=") + 1, line.Length - line.IndexOf("=") - 1));
                    i++;
                    line = sr.ReadLine();
                }
            }
      

  6.   

    你这种东西,有这么难吗?
    string a = "company=Hello world";
                int i = a.IndexOf('=');
                if(i >= 0)
                {
                    string v = a.Substring(i + 1);
                    MessageBox.Show(v);
                }
      

  7.   

    如果是读键值对(ini文件),在系统API中有标准的函数的,如:
    DWORD GetPrivateProfileString(
      LPCTSTR lpAppName,        // section name
      LPCTSTR lpKeyName,        // key name
      LPCTSTR lpDefault,        // default string
      LPTSTR lpReturnedString,  // destination buffer
      DWORD nSize,              // size of destination buffer
      LPCTSTR lpFileName        // initialization file name
    );
    还有其他一系列以GetPrivateProfile开头的函数,查一下就有了。
      

  8.   

    一行一行获取,然后split('=')分拆