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
  • 489,969
  • 99
  • 883
  • 1,009
Michael Quiles
  • 1,131
  • 3
  • 24
  • 41

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
  • 489,969
  • 99
  • 883
  • 1,009
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
  • 54,199
  • 15
  • 94
  • 116
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
  • 97,872
  • 84
  • 296
  • 452