Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of ToTitleCase

Tags:

c#

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

like image 247
ivor Avatar asked Nov 11 '09 15:11

ivor


People also ask

What is toTitleCase?

Use the toTitleCase function to capitalize the first letter of every word in a Text value. The toTitleCase function returns a Text value.

Which method should convert string into title case?

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.

How do you properly string a case in C#?

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.

How do you capitalize each word in C#?

In C#, the Toupper() function of the char class converts a character into uppercase.


2 Answers

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.

like image 174
Alfero Chingono Avatar answered Oct 12 '22 18:10

Alfero Chingono


ToTitleCase is defined in the TextInfo class, that you can reach through the current CultureInfo:

CultureInfo.CurrentCulture.TextInfo.ToTitleCase(item.Text)
like image 25
Fredrik Mörk Avatar answered Oct 12 '22 19:10

Fredrik Mörk