Convert mixed-format phone numbers to pure numbers I am just getting started with C# and wanted to make sure that I am aware of any errors/inefficiencies in my code.

For this specific project, I wanted to prove to someone that a "bot" could easily understand phone numbers from Craigslist that are mixed (ex. 9eight7six54three2one0).

Here is what I have right now - it successfully outputs a string of numbers when fed a phone number such as the one above.

using System;

namespace Phone_Decoder
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter your mixed-format phone number:");

            string phone = Console.ReadLine();

            char[] b = new char[phone.Length];

            b = phone.ToCharArray();

            string finalphone = string.Empty;

            string number = string.Empty;


I am wondering if this "number" string should be a character array or not. It seems much easier to append a string than to add characters to an array, but I thought I read that strings are more memory-intensive.

            foreach (char c in b)
            {
                if (char.IsDigit(c))
                    finalphone += c;
                if (!char.IsDigit(c))
                {
                    number += c;
                    switch (number.ToLower())
                    {
                        case "zero":
                            finalphone += '0';
                            number = string.Empty;
                            break;
                        case "one":
                            finalphone += '1';
                            number = string.Empty;
                            break;
                        case "two":
                            finalphone += '2';
                            number = string.Empty;
                            break;
                        case "three":
                            finalphone += '3';
                            number = string.Empty;
                            break;
                        case "four":
                            finalphone += '4';
                            number = string.Empty;
                            break;
                        case "five":
                            finalphone += '5';
                            number = string.Empty;
                            break;
                        case "six":
                            finalphone += '6';
                            number = string.Empty;
                            break;
                        case "seven":
                            finalphone += '7';
                            number = string.Empty;
                            break;
                        case "eight":
                            finalphone += '8';
                            number = string.Empty;
                            break;
                        case "nine":
                            finalphone += '9';
                            number = string.Empty;
                            break;
                    }


I am a bit concerned with this switch statement, because there are many repeated lines ("number = string.Empty"). Could I make it more compact somehow?

                }
            }
            Console.WriteLine(finalphone);
            Console.ReadLine();
        }
    }
}
You can extract that logic to a simple dedicated method. 

Your solution is over-complicated for such a simple task so I will offer an alternative solution.

Let's inspect the problem - 

You get some string as an input. This string might contain digits and it might also contain words that represent numbers. If there are such words we want to replace them with the respective digit.

We humans know that 'Zero' means 0 and 'One' means 1 because this is part of our vocabulary, if we are uncertain of some word we can look it up in the dictionary right? The human dictionaries are structured mostly in the following way - Word - Meaning.

Well luckily C# also has such data type. Conveniently enough it's called Dictionary! Using this data type we can construct much more readable and extendable code.

Let's start by declaring a Dictionary:

private static readonly Dictionary&lt;string, int&gt; numbers = new Dictionary&lt;string, int&gt;
{
    ["Zero"] = 0,
    ["One"] = 1,
    ["Two"] = 2,
    ["Three"] = 3,
    ["Four"] = 4,
    ["Five"] = 5,
    ["Six"] = 6,
    ["Seven"] = 7,
    ["Eight"] = 8,
    ["Nine"] = 9,
};


From here it's trivial to write a short method that does the job utilizing the dictionary numbers:

private static string ConvertFromMixedToStandardNumbers(string input)
{
    string result = input;
    foreach (var number in numbers)
    {
        result = result.Replace(number.Key.ToLower(), number.Value.ToString());
    }
    return result;
}


Or even shorter with LINQ:

private static string ConvertFromMixedToStandardNumbers(string input)
{
    return numbers.Aggregate(input,
        (current, number) =&gt; current.Replace(number.Key.ToLower(), number.Value.ToString()));
}


As you can see it's extremely easy to write short and humanly understandable code when you use the proper data structures and C# techniques.

You can even add an enum to the mix and create the dictionary from it:

private enum Number
{
    Zero,
    One,
    Two,
    Three,
    Four,
    Five,
    Six,
    Seven,
    Eight,
    Nine,
}

private static readonly Dictionary&lt;string, int&gt; numbers = MapEnum(typeof(Number));

private static Dictionary&lt;string, int&gt; MapEnum(Type enumType)
{
    return Enum.GetValues(enumType).Cast&lt;object&gt;()
        .ToDictionary(value =&gt; value.ToString(), value =&gt; (int) value);
}