2010-05-16 40 views
4

我在這裏做錯了什麼?我希望用戶名稱在輸出中顯示爲propercase,但我無法弄清楚。正確的案例標題案例問題

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(); 
+0

你能告訴我們你得到了什麼和你的期望嗎? – 2010-05-16 15:20:23

+0

當用戶輸入「JOHN DOE」時,我得到了「JOHN DOE」它應該將其轉換爲「John Doe」,但我不知道什麼即時做錯了 – 2010-05-16 15:23:24

+0

這就是問題所在,先用ToLower先將字符串小寫,然後標題框將工作。 – 2010-05-16 15:27:27

回答

8

我已經測試上的全部大寫的字下面在它的工作原理:

string proper = "TEST STRING"; 
CultureInfo properCase = System.Threading.Thread.CurrentThread.CurrentCulture; 
TextInfo currentInfo = properCase.TextInfo; 
proper = currentInfo.ToTitleCase(currentInfo.ToLower(proper)); 
// proper = "Test String" 

所以 - 改變字符串調用ToTitleCase之前降低的情況。

MSDN文檔確實表示全部大寫的字符串(例如首字母縮略詞)不會被轉換,並且後文中提供的示例代碼也證實了這一點。

+0

非常感謝他們工作的人! – 2010-05-16 15:30:51

+0

請看看@ http://stackoverflow.com/questions/4426439/function-to-normalize-string-case-according-to-specified-or-current-cultureinfo – Shimmy 2010-12-13 07:11:14