Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper Case Title Case Question

Tags:

c#

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 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(); 
like image 203
Michael Quiles Avatar asked Dec 17 '22 00:12

Michael Quiles


2 Answers

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.

like image 136
Oded Avatar answered Dec 24 '22 02:12

Oded


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.

like image 36
Hans Olsson Avatar answered Dec 24 '22 00:12

Hans Olsson