/// <summary>
/// method to convert a string to proper case
/// </summary>
/// <param name="str">value we want converted</param>
/// <returns></returns>
public static string FormatProperCase(this string str)
{
StringBuilder sb = new StringBuilder(str.Length);
// if not value is provided throw an exception
if (string.IsNullOrEmpty(str))
throw new ArgumentException("A null value cannot be converted", str);
//if the stirng is less than 2 characters return it upper case
if (str.Length < 2)
return str.ToUpper();
//let's start with the first character (make upper case)
sb.Append(str.Substring(0, 1).ToUpper());
//now loop through the rest of the string converting where needed
for (int i = 1; i < str.Length; i++)
{
if (Char.IsUpper(str[i]))
sb.Append(" ");
else
sb.Append(str[i]);
}
//return the formatted string
return sb.ToString();
}
Edited by PsychoCoder, 25 August 2010 - 05:16 PM.


Sign In
Create Account



Back to top









