把TXT文件做数据源连接,读取每一行数据,用split(',')分开每个数字,存入数组.

解决方案 »

  1.   

    string s=@"1,2,3,4,1,
    1.1,2.2,3.3,";
    string[] d=s.Split(new char[]{','});
    剩下的就是通过Convert.ToDouble() 转换成数字了
      

  2.   

    使用正则表达式
    float[] ff;
    string yourStr = "1,2,3,4,1,1.1,2.2,3.3,";
    string regexStr = @"[-+]?\b(?:[0-9]*\.)?[0-9]+\b";
    //StreamReader sr = new StreamReader(yourFile, Encoding.Default);
    //yourStr = sr.ReadToEnd();
    MatchCollection mc = Regex.Matches(yourStr, regexStr);
    int length = mc.Count;
    ff = new float[length];
    for(int i = 0; i < length; ++i)
    {
        ff[i] = float.Parse(mc[i].Value);
    }
      

  3.   

    StreamReader sr = new StreamReader("test.txt");
    string[] str = sr.ReadToEnd().Trim('\r','\n',' ').Split(',');
    sr.Close();
    double[] dou = new double[str.Length];
    for (int i = 0; i < str.Length; i++)
    {
    dou[i] = double.Parse(str[i]);
    }
      

  4.   

    也可以:
    StreamReader sr = new StreamReader("test.txt");
    string[] str = sr.ReadToEnd().Split(',','\r','\n',' ');
    sr.Close();
    double[] dou = new double[str.Length];
    for (int i = 0; i < str.Length; i++)
    {
    dou[i] = double.Parse(str[i]);
    }
    这样的话换行和空格都看作是数字与数字的分隔符
      

  5.   

    用正则,或者用split函数都不错:) up
      

  6.   

    使用string.Split(',')放入arraylist中。因为你文件中有多少行是不知道的。
    ArrayList GetAllFloatFromFile( string filePath )
    {
       try
       {
          FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
          StreamReader sr = new StreamReader(fs);
          string line = sr.ReadLine();
          ArrayList floatArrayList = new ArrayList();
          while( line != null )
          {
              string[] strs = line.Split(",");
              for(int i = 0; i<strs.Length; i++)
                  floatArrayList.Add( Convert.ToDouble( strs[i] ) );
          }
       }
       catch( Exception ex )
       {
        
       }
       finally
       {
          sr.Close();
          fs.Close();
          return floatArrayList; 
       }
    }
      

  7.   

    修改一下:
    Public static ArrayList GetAllFloatFromFile( string filePath )
    {
       try
       {
          FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
          StreamReader sr = new StreamReader(fs);
          string line = sr.ReadLine();
          ArrayList floatArrayList = new ArrayList();
          while( line != null )
          {
              string[] strs = line.Split(",");
              for(int i = 0; i<strs.Length; i++)
                  floatArrayList.Add( Convert.ToDouble( strs[i] ) );
              line = sr.ReadLine();
          }
       }
       catch( Exception ex )
       {
        
       }
       finally
       {
          sr.Close();
          fs.Close();
          return floatArrayList; 
       }
    }
      

  8.   

    StreamReader sr = new StreamReader("test.txt");
    string[] str = sr.ReadToEnd().Trim('\r','\n',' ').Split(',');
    sr.Close();
    double[] dou = new double[str.Length];
    for (int i = 0; i < str.Length; i++)
    {
    dou[i] = double.Parse(str[i]);
    }
    这是将整个文本全部读取出来后处理的,如果想一行行处理,
    将第二行的ReadToEnd()换成ReadLine()//查看看,大致这样拼写