aaronvan icon

SplitStringSolution

aaronvan | PRO | 06/03/18 07:11:56 PM UTC | 0 ⭐ | 516 👁️ | Never ⏰ | []
C# |

1.17 KB

|

None

|

0 👍

/

0 👎

namespace SplitStringSolution
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(CamelCase(" golf charlie mike foxtrot"));
            Console.WriteLine(CamelCase("bing bong boom"));
            Console.WriteLine(CamelCase("smart radio home enterprise  "));
            Console.ReadKey();
        }
 
        public static string CamelCase(string str)
        {
            if (string.IsNullOrWhiteSpace(str))
                return str;
 
            //remove leading/trailing whitespace
            string newString = str.Trim();
 
            string finalString = "";
 
            // split the string into an array of substrings delimited by whitespace
            string pattern = @"\s+";
            String[] substrings = Regex.Split(newString, pattern);
 
            //capitalize first char of each substring
            // append the first substring letter and rest of the substring to newString
            foreach (var s in substrings)
            {
                finalString += s.Substring(0, 1).ToUpper() + s.Substring(1, s.Length - 1);
            }
            return finalString;
        }
    }
}

Comments