4

first letter我想用它所在的某种格式来格式化一个字符串值Uppercase
例如:

string.Format("{0}", "myName"); //Output must be : "MyName"

我该怎么做?

4

5 回答 5

5

请查看 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));
于 2012-11-06T07:31:17.427 回答
3

如果您只想将第一个字母大写,也许:

string s = string.Format("{0}", char.ToUpper(myname[0]) + myname.Substring(1));

否则,将每个单词大写可能使用TextInfo.ToTitleCase?

string s = string.Format("{0}",
    CultureInfo.CurrentCulture.TextInfo.ToTitleCase(myname));
于 2012-11-06T07:30:05.673 回答
2

尝试

string test = "myname";
string formatted = System.Globalization.CultureInfo
                         .CurrentUICulture.TextInfo.ToTitleCase(test);
于 2012-11-06T07:28:58.060 回答
2
CultureInfo cultureInfo   = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;

Console.WriteLine("{0}", textInfo.ToTitleCase(myname));
于 2012-11-06T07:36:02.113 回答
1
string input = "myname";
var charArray = input.ToArray();
charArray[0] = char.ToUpper(charArray[0]);

string result = new string(charArray);
于 2012-11-06T07:34:18.667 回答