Proper Case Title Case Question
What am i doing wrong here? I want the users name to be shown in the output as propercase but I cant figure it out.
string proper = this.xTripNameTextBox.Text;
CultureInfo properCase = System.Threading.Thread.CurrentThread.CurrentCulture;
TextInfo currentInfo = properCase.TextInfo;
proper = currentInfo.ToTitleCase(proper);
this.xTripOutputLabel.Text = proper + Environment.NewLine + "The total gallons 开发者_Python百科you would use: " + Output.ToString("0") + Environment.NewLine + "Total amount it will cost you: " + Coutput.ToString("C") + Environment.NewLine +" Your customer number is " + rnd1.Next(1, 1000).ToString();
I have tested the following on an all upper case word at it works:
string proper = "TEST STRING";
CultureInfo properCase = System.Threading.Thread.CurrentThread.CurrentCulture;
TextInfo currentInfo = properCase.TextInfo;
proper = currentInfo.ToTitleCase(currentInfo.ToLower(proper));
// proper = "Test String"
So - change the string to lower case before calling ToTitleCase
.
The MSDN documentation does say that a string that is all upper case (such as an acronym) will not be converted and the sample code provided in the post corroborates this.
That's according to spec, quote from the doc: However, this method does not currently provide proper casing to convert a word that is entirely uppercase
http://msdn.microsoft.com/en-us/library/system.globalization.textinfo.totitlecase.aspx
Without testing I'd guess that you could do it by first making it LowerCase
and then TitleCase
.
Seems right, I am using
return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(text);
And it's working.
Try to force another culture info.
See Also
- How to capitalize the first character of each word, or the first character of a whole string, with C#?
精彩评论