How to convert a char array to a string array?

后端 未结 4 1243
一向
一向 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:31

    You can do this:

                var dayCode = "MWF";
                var daysArray = new List();
                var list = new Dictionary{
                    {"M", "Monday"},
                    {"T", "Tuesday"},
                    {"W", "Wednesday"},
                    {"R", "Thursday"},
                    {"F", "Friday"},
                    {"S", "Saturday"},
                    {"U", "Sunday"}
                };
    
                for(int i = 0,max = dayCode.Length; i < max; i++)
                {
                    var tmp = dayCode[i].ToString();
                    if(list.ContainsKey(tmp))
                    {
                        daysArray.Add(list[tmp]);
                    }
                }
                Console.WriteLine(string.Join(",", daysArray));
    

    Output:

    enter image description here

提交回复
热议问题