Skip to main content
Dec 27, 2016

Here is another useful C# snippet for uppercasing first word in a string.

/// <summary>
/// Method returns string with replaced first word to uppercase
/// </summary>
/// <param name="text">string text for replacing</param>
/// <returns>string replaced text wiht first word to uppercase</returns>
public static string UppercaseFirstWord(string text)
{
  string firstWord = String.Empty;
  string otherWords = String.Empty;
 
  // Check for empty string.
  if (String.IsNullOrEmpty(text))
  {
    return string.Empty;
  }
 
  // Get first word from passed string
  firstWord = text.Split(' ').FirstOrDefault();
  if (String.IsNullOrEmpty(firstWord))
  {
    firstWord = text;
  }
 
  // Convert first word to uppercase
  firstWord = firstWord.ToUpper();
 
  // If there are other words get them
  if (firstWord != text)
  {
    otherWords = text.Substring(firstWord.Length);
  }
 
  return firstWord + otherWords;
}