Skip to content

Instantly share code, notes, and snippets.

@Sl4vP0weR
Created September 22, 2023 22:56
Show Gist options
  • Save Sl4vP0weR/c3ea5a18e7f38319eca50192fec781b8 to your computer and use it in GitHub Desktop.
Save Sl4vP0weR/c3ea5a18e7f38319eca50192fec781b8 to your computer and use it in GitHub Desktop.
Capitalize first letter efficiently.
public static string? CapitalizeFirstLetter(this string? text)
{
if (string.IsNullOrWhiteSpace(text)) return text;
StringBuilder result = new(text);
var length = text!.Length;
for (var i = 0; i < length; i++)
{
var c = text[i];
if (!char.IsLetter(c))
continue;
result[i] = char.ToUpper(c);
break;
}
return result.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment