first letter
我想用它所在的某种格式来格式化一个字符串值Uppercase
。
例如:
string.Format("{0}", "myName"); //Output must be : "MyName"
我该怎么做?
请查看 MSDN 以了解您的情况,请参阅TextInfo.ToTitleCase Method。
string myString = "wAr aNd pEaCe";
TextInfo myTI = new CultureInfo("en-US", false).TextInfo;
Console.WriteLine("\"{0}\" to titlecase: {1}", myString, myTI.ToTitleCase(myString));
如果您只想将第一个字母大写,也许:
string s = string.Format("{0}", char.ToUpper(myname[0]) + myname.Substring(1));
否则,将每个单词大写可能使用TextInfo.ToTitleCase
?
string s = string.Format("{0}",
CultureInfo.CurrentCulture.TextInfo.ToTitleCase(myname));
尝试
string test = "myname";
string formatted = System.Globalization.CultureInfo
.CurrentUICulture.TextInfo.ToTitleCase(test);
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;
Console.WriteLine("{0}", textInfo.ToTitleCase(myname));
string input = "myname";
var charArray = input.ToArray();
charArray[0] = char.ToUpper(charArray[0]);
string result = new string(charArray);