0

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?

xs0mE1
  • 77
  • 6
  • 1
    Does this answer your question? [Efficient way to remove ALL whitespace from String?](https://stackoverflow.com/questions/6219454/efficient-way-to-remove-all-whitespace-from-string) – Jay May 31 '22 at 11:15
  • 5
    Try following : content.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); – jdweng May 31 '22 at 11:16
  • This question is clearly about Unity 3D given the `EditorUtility.OpenFilePanel`. Please don't remove the tag –  May 31 '22 at 12:02

1 Answers1

2

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.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]);
}
Orace
  • 7,822
  • 30
  • 45
Kirjava
  • 226
  • 1
  • 11
  • you can call the replace on a string. Need a foreach loop to do this on each element of your string table. It should not be needed with your split as you won't retrieve whitespaces. It's juste the Replace function that doesn't exist as you don't call it on a string. – Kirjava May 31 '22 at 11:22
  • should I loop on words.Length or contents – xs0mE1 May 31 '22 at 11:29
  • I addded a code that would do the job – Kirjava May 31 '22 at 11:33
  • That wouldnt work as my code supposed to skip first two lines as the text files aren't the same as above, there are two lines on top which I've removed. – xs0mE1 May 31 '22 at 11:42
  • Sorry, I didn't see. Anyway the for wasn't the problem in here. If you change the foreach loop into a for loop starting at 2 (as you did above), it will also work. – Kirjava May 31 '22 at 11:44
  • Everything is fine now, thanks for your help <3 Appreciated! – xs0mE1 May 31 '22 at 11:47
  • 1
    `foreach (string line in lines.Skip(2))` would work to start processing at the 3rd line (you may need a `using System.Linq;` at the top of the file) – Hans Kesting May 31 '22 at 12:00