I am trying to make it so when a user enters a value and submits it, it is stored with the first letter per word capitalized and the rest lower case. I want to do it for model.Name in:
@Html.EditorFor(model => model.Name)
I found this neat function that does what I want, but I can't for the life of me figure out how to combine the two:
s = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(s.toLower());
I would seriously appreciate any help, I have been working on this forever and nothing to show for it yet.
Considering that your string is in a variable called "strSource", then you can do something like this:
char.ToUpper(strSource[0]).ToString() + strSource.Substring(1).ToLower();
Or, the better solution would be to create an extension method:
public static string ToUpperFirstLetter(this string strSource)
{
if (string.IsNullOrEmpty(strSource)) return strSource;
return char.ToUpper(strSource[0]).ToString() + strSource.Substring(1).ToLower();
}
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