這裏是一個可以使用的擴展方法。它支持當前的文化,或允許你傳遞文化。
使用:
cities.Add(row[0].ToString().ToTitleCase()
public static class StringExtension
{
/// <summary>
/// Use the current thread's culture info for conversion
/// </summary>
public static string ToTitleCase(this string str)
{
var cultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
return cultureInfo.TextInfo.ToTitleCase(str.ToLower());
}
/// <summary>
/// Overload which uses the culture info with the specified name
/// </summary>
public static string ToTitleCase(this string str, string cultureInfoName)
{
var cultureInfo = new CultureInfo(cultureInfoName);
return cultureInfo.TextInfo.ToTitleCase(str.ToLower());
}
/// <summary>
/// Overload which uses the specified culture info
/// </summary>
public static string ToTitleCase(this string str, CultureInfo cultureInfo)
{
return cultureInfo.TextInfo.ToTitleCase(str.ToLower());
}
}
加正則表達式的標記,這樣的問題可以通過正則表達式的專家(不是我)找到; - ) –
已經回答在http://stackoverflow.com/questions/72831/how-do-i-capitalize-first-letter-of-first-name-and-last-name-in-c – dg90