Home > Back-end >  C# data structure to get months
C# data structure to get months

Time:12-28

I've got a (month of a) date written as e.g. 'MARS' which would need to return 03 (as it is the 3rd month).

Below are all my options for each month (3 languages):

{ "JAN", };
{ "FEV", "FEB", };
{ "MARS", "MAAR", "MÄR" };
{ "AVR", "APR" };
{ "MAI", "MEI" };
{ "JUIN", "JUN" };
{ "JUIL", "JUL" };
{ "AOUT", "AUG" };
{ "SEPT", "SEP" };
{ "OCT", "OKT" };
{ "NOV", };
{ "DEC", "DEZ" };

Unfortunately the C# DateTime parser doesn't recognize e.g. "MAAR" (it does recognize "MARS") so I guess I'd have to write something myself.

What is the proper way to data structure this? I was thinking of a jagged array or a list within list.

With a jagged array:

        string[][] jagged_array = new string[12][];
        jagged_array[0] = new string[1];
        jagged_array[0][0] = "01";
        jagged_array[0][1] = "JAN";
        jagged_array[1] = new string[2];
        jagged_array[1][0] = "02";
        jagged_array[1][1] = "FEV";
        jagged_array[1][2] = "FEB";
        jagged_array[2] = new string[3];
        jagged_array[2][0] = "03";
        jagged_array[2][1] = "MARS";
        jagged_array[2][2] = "MAAR";
        jagged_array[2][3] = "MÄR";
        jagged_array[3] = new string[2];
        jagged_array[3][0] = "04";
        jagged_array[3][1] = "AVR";
        jagged_array[3][2] = "APR";
        jagged_array[4] = new string[2];
        jagged_array[4][0] = "05";
        jagged_array[4][1] = "MAI";
        jagged_array[4][2] = "MEI";
        (...)

Is this the recommended way of structuring the data? How do I access the month number (well, month string, which can be casted to number)?

Something like get_month("MAAR") --> should return "03". Is there an easy way to get this or do I need to loop over the individual items?

CodePudding user response:

I would create a special class, but if it is impossbile I would just use a list of string

void Main()
{
    var months = new List<string> { "01,JAN", "02,FEV,FEB", "03,MARS,MAAR,MÄR" };
    var monthName = "MARS";
    var m = GetMonthNumber(months, monthName);
}
public int GetMonthNumber(List<string> months, string monthName)
{
    return Convert.ToInt32(months.FirstOrDefault(mo => mo.Contains(monthName)).Substring(0, 2));
}

CodePudding user response:

I suggest using dictionary as collection: Key is month name, Value its numeric value, i.e.

    private static Dictionary<string, int> knownMonths =
      new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase) {
        { "Jan", 1 },
        { "Feb", 2}, { "Fev", 2},
        { "Mars", 3}, { "Maar", 3}, { "MÄR", 3}, 
        //TODO: Add more pairs here
    };

then you can implement get_month like

    // Either valid month value in 1..12 range or 0
    public int get_month(string name) => knownMonths
      .TryGetValue(name?.Trim(), out int value) ? value : 0;
    

CodePudding user response:

A simple Dictionary can do the job

int GetMonthFromString(string str)
{
    var monthNames = new Dictionary<string, int>();
    monthNames.Add("JAN", 1);

    monthNames.Add("FEB", 2);
    monthNames.Add("FEV", 2);
....
    if (!monthNames.ContainsKey(str))
        throw new ApplicationExcepion("invalid string for month"); //handle somehow an invalid string

    return monthNames[str];

}
  •  Tags:  
  • c#
  • Related