我有如下文本檔案:
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
我要做的是忽略那些白色的空白,有什么解決辦法嗎?
uj5u.com熱心網友回復:
C# 中的 replace 函式將一個字串作為輸入,并將修改后的字串作為輸出。要從字串中洗掉空格,請執行以下操作:
string myText = "1 2 3";
myText = myText.Replace(" ", "");
您的代碼將不起作用,因為您沒有在要替換空格的字串中說。
這是我的做法:
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]);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/483514.html