I have a .txt file that is read in. I can get the whole file to read in and print in a gui box, but I need to be able to use text.split(). and convert the string to an int or float depending on what the value is. I have tried int.tryParse(); but it wouldn’t take the String[ ] from using the text.split() and I would also like to read in a line at a time.
I got it to separate each line into a string but i need to separate each line by commas. then still convert the string to a int to store it.
Oddly, just by searching for parseInt in the documentation, I get this:
Just split your split.
void ReadContents(string file)
{
// split by newline
string[] lines = file.Split(System.Environment.NewLine);
foreach (string line in lines)
{
// split each line by comma
string[] values = line.Split(',');
foreach (string value in values)
{
// try to parse each comma-separated value to integer
int intValue;
if (int32.TryParse(value, out intValue))
{
// save the value or do
// whatever else you want to do with the value
}
}
}
}
Thanks KelsoMRK, that is exactly what I needed to do.