How to convert a char array to a string array?

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

    char[] daysCodeArray = days.ToCharArray();
    string[] daysArray = daysCodeArray.Select(el =>
    {
        switch (el)
        {
            case 'M':
                return "Monday";
    
            case 'T':
                return "Tuesday";
    
            case 'W':
                return "Wednesday";
    
            case 'R':
                return "Thursday";
    
            case 'F':
                return "Friday";
    
            case 'S':
                return "Saturday";
    
            case 'U':
                return "Sunday";
         }
         throw new ArgumentException("Invalid day code");
    }).ToArray();
    

    You can change the lambda into a separate method if you want.

提交回复
热议问题