11

Given:

A string dayCodes (i.e. "MWF" or "MRFU") that I need to split and create a collection of strings so I can have a list of day of the week strings (i.e. "Monday", "Wednesday", "Friday" or "Monday", "Thursday", "Friday", "Sunday").

// this causes a run-time exception because you can't cast Char to String
var daysArray = days.ToCharArray().Cast<string>().ToArray();

// for each dayCode, overwrite the code with the day string.
for (var i = 0; i < daysArray.Length; i++)
{
    switch (daysArray[i])
    {
        case "M":
            daysArray[i] = "Monday";
            break;

        case "T":
            daysArray[i] = "Tuesday";
            break;

        case "W":
            daysArray[i] = "Wednesday";
            break;

        case "R":
            daysArray[i] = "Thursday";
            break;

        case "F":
            daysArray[i] = "Friday";
            break;

        case "S":
            daysArray[i] = "Saturday";
            break;

        case "U":
            daysArray[i] = "Sunday";
            break;
    }
 }

 daysArray[daysArray.Length - 1] = "and " + daysArray[daysArray.Length - 1];

 return string.Join(", ", daysArray);

Problem:

The problem is that you can't cast Char to String which I guess makes sense because one is not inherited from the other. Still you'd think that the compiler would cast the Char as a one character long String.

Is there a quick way (like using Cast<string>()) to do this so I don't have to create a List<string> from scratch?

4 Answers 4

28

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<char, string> that maps to the full day name:

Dictionary<char, string> dayMapping = new Dictionary<char,string>()
{
    {  'M', "Monday" },
    {  'T', "Tuesday" }
    //and so on
}
Sign up to request clarification or add additional context in comments.

2 Comments

It'd be worth considering using the System.DayOfWeek enum in place of a magic string here, too.
@uglybugger I agree, except that the point of the method I'm making is to concatenate strings so it just be more code (i.e. having to call .ToString() on everything.
2
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.

Comments

1

You can do this:

            var dayCode = "MWF";
            var daysArray = new List<string>();
            var list = new Dictionary<string, string>{
                {"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

Comments

1

To answer the question in the title for anyone finding this in a search...(not the problem as described...thats one of earlier posts.)

var t = "ABC";
var s = t.ToCharArray().Select(c => c.ToString()).ToArray();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.