4

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(); 
Oded
  • 477,625
  • 97
  • 867
  • 998
Michael Quiles
  • 1,131
  • 3
  • 24
  • 40

3 Answers3

8

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.

Oded
  • 477,625
  • 97
  • 867
  • 998
  • Please take a look @ http://stackoverflow.com/questions/4426439/function-to-normalize-string-case-according-to-specified-or-current-cultureinfo – Shimmy Weitzhandler Dec 13 '10 at 07:11
3

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.

Hans Olsson
  • 53,038
  • 14
  • 91
  • 113
1

Seems right, I am using

return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(text);

And it's working.

Try to force another culture info.

See Also

Community
  • 1
  • 1
BrunoLM
  • 94,090
  • 80
  • 289
  • 441