- Home /
Simple Data Parsing from Text Files
I have a text file that I'm loading into a game to control some timing in my game. All the information is integers (anywhere from 0 to several million) separated by commas, semicolons, and pipes (this thing for people who don't know |). Is there an easy way of getting the information int by int or do I have to go through and get it character by character and test to see what I'm getting with each one?
Thanks
Example of text file:
1,10;2,100;3;200;4,300;5,400;6,500;7,600;8,700;9,800;10,900|
Answer by Arkaid · Aug 15, 2016 at 05:20 AM
Here you go
void ParseFile()
{
string text = File.ReadAllText("YourTextFile");
char[] separators = { ',', ';', '|' };
string[] strValues = text.Split(separators);
List<int> intValues = new List<int>();
foreach(string str in strValues)
{
int val = 0;
if (int.TryParse(str, out val))
intValues.Add(val);
}
}
This will return a list of integers in the same order as in the file.
If you need to group them by type of separator, just use Split sequentially (for example: split semicolons first, then for each group, split by commas)
Your answer
Follow this Question
Related Questions
How do I insert a variable into a URL? Please Help! 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
how to grab a specific int without converting it to string? 3 Answers
Planning Text Scroll with Unity UI 0 Answers