Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用linq解析文本_C#_String_Linq - Fatal编程技术网

C# 用linq解析文本

C# 用linq解析文本,c#,string,linq,C#,String,Linq,我有这样的文本 5 1 .021 -56.6 -5 0.4 -5 0. -.05 -.1 .05 -.1 .05 .1 -.05 .1 YESA 1. .8507 .84993 我必须去 5

我有这样的文本

      5       1     .021   -56.6   -5      0.4    -5      0.    
   -.05     -.1     .05     -.1     .05      .1    -.05      .1       
   YESA      1.                                                       
  .8507  .84993
我必须去

      5       1     .021   -56.6   -5      0.4   -5      0.    
   -.05     -.1     .05     -.1     .05      .1    -.05      .1       
   YESA      1.     0.       0.      0.      0.      0.      0.               
  .8507  .84993     0.       0.      0.      0.      0.      0. 
但是当我使用下一个结构时

   FileStream fs = new FileStream(fileName, FileMode.Open);
            StreamReader sr = new StreamReader(fs);
         while (!sr.EndOfStream)
        {
            string line = sr.ReadLine();
            string temp = System.Text.RegularExpressions.Regex.Replace(line, @"\s+", " ");
            string[] vector = temp.Split(' ');
            for (int i = 0; i < vector.Length; i++)
                Console.WriteLine(ConvertToFloat(ConvertString(vector[i])));
        }
FileStream fs=newfilestream(文件名,FileMode.Open);
StreamReader sr=新的StreamReader(fs);
而(!sr.EndOfStream)
{
字符串行=sr.ReadLine();
字符串temp=System.Text.RegularExpressions.Regex.Replace(第行@“\s+”,“”);
字符串[]向量=温度拆分(“”);
对于(int i=0;i

我得到的第一个文本没有变化。

我使用两个辅助函数

private float ConvertToFloat(string line)//to check integer or float number
        {
            string temp = "";

            if (line[line.Length - 1] == ',' || line[0] == ',')
            {
                temp = line.Replace(',', '\0');
                return float.Parse(temp);
            }
            else
                return float.Parse(line);

        }
        private string ConvertString(string line)//change '.' to ','
        {
            return line.Replace('.', ',');
        }

我不明白你为什么还要费心去分析浮动

看起来您只有一行项目(可能是浮动项目,也可能不是浮动项目),您必须完成8行项目。像

        foreach (string line in lines)
        {
            var words = line.Split( new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            foreach(string w in words)
                Console.Write("{0,6}", w);

            // filling out
            for (int i = words.Length; i < 8; i++)
                Console.Write("{0,6}", "0.");

            Console.WriteLine();
        }
foreach(行中的字符串行)
{
var words=line.Split(新字符[]{''},StringSplitOptions.RemoveEmptyEntries);
foreach(单词中的字符串w)
Console.Write(“{0,6}”,w);
//填写
for(inti=words.Length;i<8;i++)
Console.Write(“{0,6}”,“0”);
Console.WriteLine();
}

问题是什么还不清楚。。。或者确实是为什么这里的很多代码都会使用逗号和零字符,因为这似乎和您要做的事情完全无关。你能澄清一下吗:这里的间距是多少?要对齐的空间?还是标签?或者…?你们好像忘了问一个问题,在课文中,我有一个数字序列,在数字的位置,我有一个空格,我必须把每个空格换成零。每一行我都有7个数字越来越近。但是如何指定字段(宽度)以及如何从
0.4.5794-5
0.4.5
-5
?大多数字段似乎是右对齐的,但在这个字段中不清楚。我有错误,我纠正了它。