bf. a bf. a - 28 days ago 6
C# Question

I wanna get values from TextFile C#

I wanna get a couple of values from my textfile in C#

1.sex=male
1.name=barack
1.lastname=obama
1.age = 55

2.sex=female
2.name= kelly
2.lastname=clinton
2.age = 24

3.sex = male
3.firstname= mike
3.lastname= james
3.age= 19


i only wanna get all the names lastnames and ages from the textFile not the sex how can i filter this i have something like this but this only shows 1 value.

var list = new List<string>();

var text = File.ReadAllLines(@"C:\Users\Jal\Desktop\Test.text");
foreach (var s in text)
{
if (s.Contains("Name"))
{
if (s.Contains("Name"))
{
var desc = s.Substring(s.IndexOf("=") + 1);
list.Add(desc);
ListView.Items.Add(desc);
}
}
}


Found this code on Stackoverflow but i dont get every value i want

BWA BWA
Answer
var names = new List<string>();
var lastnames = new List<string>();

var text = File.ReadAllLines(@"C:\Users\Jal\Desktop\Test.text");
foreach (var s in text)
{
    if (s.Contains("lastname"))
    {        
        var lastname = s.Substring(s.IndexOf("=") + 1);
        lastnames.Add(desc);
        continue;
    }
    if (s.Contains("name"))
    {        
        var name = s.Substring(s.IndexOf("=") + 1);
        names.Add(desc);
        continue;
    }        
}

And in same way you can add another properties.

Comments