I’ve text file like the one below:
C 0.000 1.397 -0.004
C 1.209 0.700 0.001
C 1.210 -0.698 0.004
I’ve done this code for it
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]);
}
Some other text files can have more spaces like the one below:
C 0.000 1.397 -0.004
C 1.209 0.700 0.001
C 1.210 -0.698 0.004
What I’ve to do is to ignore those white empty spaces, is there any solutions?
>Solution :
The replace function in C# takes a string in input and give the modified string as an output.
To remove a space from a string, please do the following :
string myText = "1 2 3";
myText = myText.Replace(" ", "");
Your code won’t work as you don’t say in wich string you wanna replace the white space.
Here is how I would do it :
var lines = File.ReadAllLines(path);
foreach (string line in lines)
{
string[] words = line.Split(new[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries); // removes the empty results
// Here you could replace spaces but it isn't needed.
// That could be done this way : words[0] = words[0].Replace(" ", "");
float x = float.Parse(words[0]); // your table starts at 0
float y = float.Parse(words[1]);
float z = float.Parse(words[2]);
}