The .NET Framework has a method TextInfo.ToTitleCase.
Is there something equivalent in .NET Core?
Edit: I'm looking for a built-in method in .NET Core.
You can implement your own extension method:
public static class StringHelper
{
public static string ToTitleCase(this string str)
{
var tokens = str.Split(new[] { " ", "-" }, StringSplitOptions.RemoveEmptyEntries);
for (var i = 0; i < tokens.Length; i++)
{
var token = tokens[i];
tokens[i] = token == token.ToUpper()
? token
: token.Substring(0, 1).ToUpper() + token.Substring(1).ToLower();
}
return string.Join(" ", tokens);
}
}
Credit: blatently copied form this gist*.
*Added the bit for acronyms Dotnet Fiddle.
It seems there is no such method built-in to .NET Core.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With