2011-01-06 29 views
5

//例如如何使用c#代碼製作每個單詞首字母的第一個字母

string s =「這是例子」;

//我怎麼能做出這樣的輸出「這是實例」在C#中使用過於簡單的代碼

+0

重複:http://stackoverflow.com/questions/1943273/convert-all-first-letter-to-upper-case-rest-每個詞較低 – 2011-01-06 07:11:02

+0

@CD:細微差別。 – leppie 2011-01-06 07:13:50

回答

7

你描述的內容有時被稱爲ProperCase,或者在C#的情況下,TitleCase。這看起來似乎過火了,但據我所知,它需要一些「文化」本地化信息。幸運的是,你可以默認使用當前正在使用的那個。

CultureInfo c = Thread.CurrentThread.CurrentCulture; 
TextInfo textInfo = c.TextInfo; 

String newString = textInfo.ToTitleCase(oldString); 

當然,在實踐中,你可能會想要把它所有在一起,就像Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase,但它不能傷害,看看所有的垃圾手段。

http://support.microsoft.com/kb/312890

10

試試這個。

String s = "this is example"; 
Console.WriteLine(Thread.CurrentCulture.TextInfo.ToTitleCase(s)); 
0

嘗試使用以下代碼

Console.WriteLine(System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(str)); 
相關問題