Uppercase Word Method in C#

If you want to uppercase a passed in word using c#, this is a method I found and use it in my helpers classes.

Unfortunately, I can’t find the site for this anymore so I am writing this here as a keep sake for myself and others.

If you happen to know where this site was for this code, please let me know so I can give proper attribution.

private static string UppercaseWords(string value)
{
	char[] array = value.ToCharArray();
	// Handle the first letter in the string.
	if (array.Length >= 1)
	{
		if (char.IsLower(array[0]))
		{
			array[0] = char.ToUpper(array[0]);
		}
	}
	// Scan through the letters, checking for spaces.
	// ... Uppercase the lowercase letters following spaces.
	for (int i = 1; i < array.Length; i++)
	{
		if (array[i - 1] == ' ')
		{
			if (char.IsLower(array[i]))
			{
				array[i] = char.ToUpper(array[i]);
			}
		}
	}
	return new string(array);
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.