Friday, July 25, 2014

Easily split a string with Upper Case Letters

I am often finding myself having to take one long string, containing Upper and Lower case letters, and having to split it up by the Upper Case letter to make up the words. Here is a quick and easy way to do so:


string base_string = "ThisIsMyString";
string formated_string = ""; //This will hold our result of This Is My String
Array.ForEach
     (base_string.ToCharArray() // Take the base string and create an array of each charater
     , x=>
         {
         if(char.IsUpper(x))
         {
              formated_string += " ";
         };
         formated_string = string.Concat(formated_string , x.ToString());
         }
     );

Response is This Is My String