# Wednesday, December 23, 2009

String.Humanize()

Another extension method… I just ran into the need to humanize some strings into more friendly ones. This can be used to humanize the name of an enumeration. It will turn a “CamelCasedString” into a “Camel Cased String”.

public static class StringExtension
{
  
/// <summary>
   /// Humanize a "CamelCasedString" into "Camel Cased String".
   /// </summary>
   /// <param name="source"></param>
   /// <returns></returns>
   public static string Humanize(this string source )
    {
      
StringBuilder sb =new StringBuilder();

      
char last =char.MinValue;
      
foreach (char cin source )
        {
          
if (char.IsLower( last ) ==true &&char.IsUpper( c ) ==true )
            {
                sb.Append(
' ' );
            }
            sb.Append( c );
            last = c;
        }
      
return sb.ToString();
    }
}

#    Comments [0] |