Answered by:
C# - Reading Specific Data from a File
Question
-
Answers
-
How can I do this for each line in the file, as the currently the string s only displays the last linevar files = File.ReadLines(@"data\common\country_data\United Kingdom.txt");
foreach (var f in files) {
string s = f.Replace("\"", "").Split('=')[1];
mainGameCountrySelectedPopulationValueLabel.Text = s; }
Your example here is looping around every line. But it overwrites mainGameCountrySelectedPopulationValueLabel.Text with the extracted value in s every single time. So it ends up just showing the last value.
Do you actually want the values for each other line? Or just want to scan for one specific line?
For example, in the loop you can check to see what your variable f starts with to know which line you are currently on and act appropriately:
var files = File.ReadLines(@"data\common\country_data\United Kingdom.txt"); foreach (var f in files) { if (f.StartsWith("population")) { string s = f.Replace("\"", "").Split('=')[1]; mainGameCountrySelectedPopulationValueLabel.Text = s; } }
- Marked as answer by DiogoCosta1998 Tuesday, July 7, 2015 1:00 PM
All replies
-
You should read the file line by line. Then look for the index of the first and the second quotation mark. With this indices you can extract the substring. Maybe this example helps:
var valueString = "MyValue = \"value\""; var firstIndex = valueString.IndexOf('"'); var lastIndex = valueString.LastIndexOf('"'); var length = lastIndex - firstIndex; var value = valueString.Substring(firstIndex + 1, length - 1);
---------------------------------- Robin Sedlaczek @ Microsoft Forums
-
Check this example, that will replace " with empty string and split the line value by the = and read the value of index 1:
var files = File.ReadLines(@"Yourpathtothe file\File.txt") ; foreach (var f in files) { string s = f.Replace("\"", "").Split('=')[1]; }Fouad Roumieh
- Proposed as answer by Robin Sedlaczek Tuesday, July 7, 2015 10:27 AM
-
How can I do this for each line in the file, as the currently the string s only displays the last linevar files = File.ReadLines(@"data\common\country_data\United Kingdom.txt");
foreach (var f in files) {
string s = f.Replace("\"", "").Split('=')[1];
mainGameCountrySelectedPopulationValueLabel.Text = s; }
-
How can I do this for each line in the file, as the currently the string s only displays the last lineIt depends on how you want to show this, do you want to add them all to the label separated each value on a line or what? This how to concatenate for example:
mainGameCountrySelectedPopulationValueLabel.Text = mainGameCountrySelectedPopulationValueLabel.Text + " " + s;
Fouad Roumieh
-
How can I do this for each line in the file, as the currently the string s only displays the last linevar files = File.ReadLines(@"data\common\country_data\United Kingdom.txt");
foreach (var f in files) {
string s = f.Replace("\"", "").Split('=')[1];
mainGameCountrySelectedPopulationValueLabel.Text = s; }
Your example here is looping around every line. But it overwrites mainGameCountrySelectedPopulationValueLabel.Text with the extracted value in s every single time. So it ends up just showing the last value.
Do you actually want the values for each other line? Or just want to scan for one specific line?
For example, in the loop you can check to see what your variable f starts with to know which line you are currently on and act appropriately:
var files = File.ReadLines(@"data\common\country_data\United Kingdom.txt"); foreach (var f in files) { if (f.StartsWith("population")) { string s = f.Replace("\"", "").Split('=')[1]; mainGameCountrySelectedPopulationValueLabel.Text = s; } }
- Marked as answer by DiogoCosta1998 Tuesday, July 7, 2015 1:00 PM
-
-
-
I would like a label to display each value of each line in the text file
Yes but what is the format that you want?
For example set the Label AutoSize property to False, set its width and height. The below loop will allow you to read each value and set it on a seperate line using \n
var files = File.ReadLines(@"Yourpath\File.txt"); foreach (var f in files) { string s = f.Replace("\"", "").Split('=')[1]; label1.Text =label1.Text + "\n" + s; }
Fouad Roumieh
- Edited by Fouad Roumieh Tuesday, July 7, 2015 12:01 PM
-
-
Check the below loop, that will read the title and the value:
var files = File.ReadLines(@"C:\Documents and Settings\Luis\Desktop\File.txt"); foreach (var f in files) { string title = f.Replace("\"", "").Split('=')[0]; string s = f.Replace("\"", "").Split('=')[1]; label1.Text = label1.Text + "\n" + title + ": " + s; }if you want the titles different to what is in the file you have to do replace.
You should have provided your requirements clear form the beginning, we cannot elaborate with each post.
Fouad Roumieh
- Edited by Fouad Roumieh Tuesday, July 7, 2015 12:43 PM
-
-
var files = File.ReadLines(@"data\common\country_data\United Kingdom.txt"); foreach (var f in files) { if (f.StartsWith("population")) { string s = f.Replace("\"", "").Split('=')[1]; mainGameCountrySelectedPopulationValueLabel.Text = s; } if (f.StartsWith("job_title")) { string s = f.Replace("\"", "").Split('=')[1]; mainGameCountrySelectedJobTitleValueLabel.Text = s; } if (f.StartsWith("forename")) { string s = f.Replace("\"", "").Split('=')[1]; mainGameCountrySelectedForenameValueLabel.Text = s; } if (f.StartsWith("surname")) { string s = f.Replace("\"", "").Split('=')[1]; mainGameCountrySelectedSurnameValueLabel.Text = s; } if (f.StartsWith("political_party")) { string s = f.Replace("\"", "").Split('=')[1]; mainGameCountrySelectedPoliticalPartyValueLabel.Text = s; }This works but is this the most efficient way of doing this