I wonder if anyone can help, I am trying to change something from caps to lower case with the Caps first letter, I undertsand I can use ToTitleCase - but I'm struggling to get this going;
<%= Html.Label("rblDeposit." + (i + 1).ToString(), item.Text.ToLowerInvariant())%>
I understand I need to supply a string into the ToTitleCase, but how do I do apply this to item.text part ?
I thought I could do something like this;
<%= Html.Label("rblDeposit." + (i + 1).ToString(), item.Text.ToTitleCase(item.Text))%>
Thanks
Use the toTitleCase function to capitalize the first letter of every word in a Text value. The toTitleCase function returns a Text value.
One way to convert a String to title case is by iterating through all the characters of the String. To do so, when we find a word separator we capitalize the next character. After that, we change the rest of the characters to lower case until we reach the next word separator. As we can see, we use the method Character.
C# has an inbuilt TextInfo. ToTitleCase() method that we can use to convert strings to title case: public string ToTitleCase (string str); This method exists within System.
In C#, the Toupper() function of the char class converts a character into uppercase.
If you do not want to use the current CultureInfo, you can use the static InvariantCulture:
System.Globalization.CultureInfo.InvariantCulture.TextInfo.ToTitleCase(item.Text)
Hope that helps.
ToTitleCase
is defined in the TextInfo
class, that you can reach through the current CultureInfo
:
CultureInfo.CurrentCulture.TextInfo.ToTitleCase(item.Text)
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