4

I want to format a string value by a certain format that the first letter of it be in Uppercase.
e.g:

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

How can I do it?

Hamid62
  • 183
  • 4
  • 15

5 Answers5

5

Please check MSDN for your case, see 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));
Omar
  • 16,329
  • 10
  • 48
  • 66
Uriil
  • 11,948
  • 11
  • 47
  • 68
3

If you only want to capitalise the first letter, maybe:

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

Otherwise, to capitalise each word maybe use TextInfo.ToTitleCase ?

string s = string.Format("{0}",
    CultureInfo.CurrentCulture.TextInfo.ToTitleCase(myname));
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
2

try

string test = "myname";
string formatted = System.Globalization.CultureInfo
                         .CurrentUICulture.TextInfo.ToTitleCase(test);
cuongle
  • 74,024
  • 28
  • 151
  • 206
m4ngl3r
  • 552
  • 2
  • 17
2
CultureInfo cultureInfo   = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;

Console.WriteLine("{0}", textInfo.ToTitleCase(myname));
NoobieDude
  • 93
  • 6
1
string input = "myname";
var charArray = input.ToArray();
charArray[0] = char.ToUpper(charArray[0]);

string result = new string(charArray);
cuongle
  • 74,024
  • 28
  • 151
  • 206