How to convert a char array to a string array?

后端 未结 4 1234
一向
一向 2021-02-14 01:52

Given:

A string dayCodes (i.e. \"MWF\" or \"MRFU\") that I need to split and create a collection of strings so I

4条回答
  •  灰色年华
    2021-02-14 02:32

    Just using char.ToString() would work:

    var daysArray = days.ToCharArray().Select( c => c.ToString()).ToArray();
    

    Alternatively, and a better solution in my mind why don't you use the string directly with a dictionary for the mapping:

    var daysArray = days.Select( c => dayMapping[c]).ToArray();
    

    with dayMapping just a Dictionary that maps to the full day name:

    Dictionary dayMapping = new Dictionary()
    {
        {  'M', "Monday" },
        {  'T', "Tuesday" }
        //and so on
    }
    

提交回复
热议问题