Created
September 22, 2023 22:56
-
-
Save Sl4vP0weR/c3ea5a18e7f38319eca50192fec781b8 to your computer and use it in GitHub Desktop.
Capitalize first letter efficiently.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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