Advertisement
aaronvan

SplitStringSolution

Jun 3rd, 2018
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.17 KB | None | 0 0
  1. namespace SplitStringSolution
  2. {
  3.     class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             Console.WriteLine(CamelCase(" golf charlie mike foxtrot"));
  8.             Console.WriteLine(CamelCase("bing bong boom"));
  9.             Console.WriteLine(CamelCase("smart radio home enterprise  "));
  10.             Console.ReadKey();
  11.         }
  12.  
  13.         public static string CamelCase(string str)
  14.         {
  15.             if (string.IsNullOrWhiteSpace(str))
  16.                 return str;
  17.  
  18.             //remove leading/trailing whitespace
  19.             string newString = str.Trim();
  20.  
  21.             string finalString = "";
  22.  
  23.             // split the string into an array of substrings delimited by whitespace
  24.             string pattern = @"\s+";
  25.             String[] substrings = Regex.Split(newString, pattern);
  26.  
  27.             //capitalize first char of each substring
  28.             // append the first substring letter and rest of the substring to newString
  29.             foreach (var s in substrings)
  30.             {
  31.                 finalString += s.Substring(0, 1).ToUpper() + s.Substring(1, s.Length - 1);
  32.             }
  33.             return finalString;
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement