读取文件行时忽略空格c#
ignore whitespace when reading file lines c#
我有如下所示的文本文件:
C 0.000 1.397 -0.004
C 1.209 0.700 0.001
C 1.210 -0.698 0.004
我已经完成了这段代码
string path = EditorUtility.OpenFilePanel("Molenity","","txt");
var contents = File.ReadAllLines(path);
for (int i = 2; i < contents.Length; i++)
{
string[] words = contents[i].Split(' ');
Replace(" ",""); // Thats what I've added as a solution but not working.
string type = words[0];
float x = float.Parse(words[1]);
float y = float.Parse(words[2]);
float z = float.Parse(words[3]);
}
其他一些文本文件可以有更多的空格,如下所示:
C 0.000 1.397 -0.004
C 1.209 0.700 0.001
C 1.210 -0.698 0.004
我要做的就是忽略那些空白,有什么解决办法吗?
C# 中的替换函数接受一个字符串作为输入并将修改后的字符串作为输出。
要从字符串中删除 space,请执行以下操作:
string myText = "1 2 3";
myText = myText.Replace(" ", "");
您的代码将无法工作,因为您没有在要替换白色的字符串中说 space。
这是我的做法:
var lines = File.ReadAllLines(path);
foreach (string line in lines.Skip(2))
{
string[] words = line.Split(' ', System.StringSplitOptions.RemoveEmptyEntries); // removes the empty results
string type = words[0];
float x = float.Parse(words[1]);
float y = float.Parse(words[2]);
float z = float.Parse(words[3]);
}
我有如下所示的文本文件:
C 0.000 1.397 -0.004
C 1.209 0.700 0.001
C 1.210 -0.698 0.004
我已经完成了这段代码
string path = EditorUtility.OpenFilePanel("Molenity","","txt");
var contents = File.ReadAllLines(path);
for (int i = 2; i < contents.Length; i++)
{
string[] words = contents[i].Split(' ');
Replace(" ",""); // Thats what I've added as a solution but not working.
string type = words[0];
float x = float.Parse(words[1]);
float y = float.Parse(words[2]);
float z = float.Parse(words[3]);
}
其他一些文本文件可以有更多的空格,如下所示:
C 0.000 1.397 -0.004
C 1.209 0.700 0.001
C 1.210 -0.698 0.004
我要做的就是忽略那些空白,有什么解决办法吗?
C# 中的替换函数接受一个字符串作为输入并将修改后的字符串作为输出。 要从字符串中删除 space,请执行以下操作:
string myText = "1 2 3";
myText = myText.Replace(" ", "");
您的代码将无法工作,因为您没有在要替换白色的字符串中说 space。
这是我的做法:
var lines = File.ReadAllLines(path);
foreach (string line in lines.Skip(2))
{
string[] words = line.Split(' ', System.StringSplitOptions.RemoveEmptyEntries); // removes the empty results
string type = words[0];
float x = float.Parse(words[1]);
float y = float.Parse(words[2]);
float z = float.Parse(words[3]);
}