Create a list of objects with initialized properties from a string with infos

  • A+

I have a string that looks like that:

random text 12234 another random text   User infos:   User name : John ID : 221223 Date : 23.02.2018 Job: job1  User name : Andrew ID : 378292 Date : 12.08.2017 Job: job2  User name : Chris ID : 930712 Date : 05.11.2016 Job : job3  some random text 

And this class:

class User {    public string UserName { get; set; }    public string ID { get; set; }    public string Date { get; set; }    public string Job { get; set; }    public User(string _UserName, string _ID, string _Date, string _Job)    {        UserName = _UserName        ID = _ID;        Date = _Date;        Job = _Job;    } } 

And I want to create a List of Users with informations from that string.

I have tried doing that:

   List<User> Users = new List<User>();      string Data = (the data above)     string[] lines = Data.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);      List<string> UserNames = new List<string>();     List<string> IDs = new List<string>();     List<string> Dates = new List<string>();     List<string> Jobs = new List<string>();      foreach (var line in lines)     {         if (line.StartsWith("User name : "))         {             UserNames.Add(Line.Remove(0, 12));                          }          if (Line.StartsWith("ID : "))         {             IDs.Add(Line.Remove(0, 5));         }          if (Line.StartsWith("Date : "))         {            Dates.Add(Line.Remove(0, 7));         }          if (Line.StartsWith("Job : "))         {            Jobs.Add(Line.Remove(0, 6));         }             }     var AllData = UserNames.Zip(IDs, (u, i) => new { UserName = u, ID = i });      foreach (var data in AllData)     {         Users.Add(new User(data.UserName, data.ID, "date", "job"));     } 

But I can only combine two lists using this code. Also, I have more than 4 values for each user (the string above was just a short example) .

Is there a better method? Thanks.


Since it seems to be always 4 lines of information you could go in steps of 4 with a loop through the splitted array lines. At each step you would split by colon : and collect the last item, which is the desired value:

EDIT: In this case I would suggets to look for the START of the data.

int startIndex = Data.IndexOf("User name"); 


also ends with another line of text

then you can use LastIndexOf to find the end of the important information:

int endIndex = Data.LastIndexOf("Job"); int lengthOfLastLine = Data.Substring(endIndex).IndexOf(Environment.NewLine); endIndex += lengthOfLastLine; 

and then simply take a SubString from the startindex on until the end

string [] lines = Data.Substring(startIndex, endIndex - startIndex)                      .Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); List<User> allUsers = new List<UserQuery.User>();  for (int i = 0; i < lines.Length; i+=4) {     string name = lines[i].Split(':').Last().Trim();     string ID   = lines[i + 1].Split(':').Last().Trim();     string Date = lines[i + 2].Split(':').Last().Trim();     string Job  = lines[i + 3].Split(':').Last().Trim();     allUsers.Add(new User(name, ID, Date, Job)); } 

Ahhh, and you should Trim the spaces away. This solution should be readable. The hard coded step size of 4 is actually annoying in my solution

Disclaimer: This solution works only as long as the format does not change. If the order of the lines should change, it will return false results


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: