This snippet takes a string as input and formats it to proper case
Format a string to proper case with C#
/// <summary>
/// method to convert a string to proper case
/// </summary>
/// <param name="str">value we want converted</param>
/// <returns></returns>
public string FormatProperCase(string str)
{
// 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)
string converted = str.Substring(0, 1).Trim().ToUpper();
//now loop through the rest of the string converting where needed
for (int i = 1; i < str.Length; i++)
{
converted =
(Char.IsUpper(str[i])) ?
converted += " " :
converted += str[i];
}
//return the formatted string
return converted;
}
lipasion 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.