Format string literal

I want to format a string value in a specific format in which the first letter is in Uppercase .
eg:

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

How can i do this?

+4
source share
5 answers

Please check the MSDN for your case, see the 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)); 
+5
source

If you want to use only the first letter, it is possible:

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

Otherwise, to use every word, use TextInfo.ToTitleCase ?

 string s = string.Format("{0}", CultureInfo.CurrentCulture.TextInfo.ToTitleCase(myname)); 
+3
source

to try

 string test = "myname"; string formatted = System.Globalization.CultureInfo .CurrentUICulture.TextInfo.ToTitleCase(test); 
+2
source
 CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture; TextInfo textInfo = cultureInfo.TextInfo; Console.WriteLine("{0}", textInfo.ToTitleCase(myname)); 
+2
source
 string input = "myname"; var charArray = input.ToArray(); charArray[0] = char.ToUpper(charArray[0]); string result = new string(charArray); 
+1
source

Source: https://habr.com/ru/post/1444274/


All Articles