Change the line to enter the first character in upper and lower case

I have lines like this:

var a = "abc"; var b = "DEF"; var c = "gHi"; 

Is there a function that I can apply to a string to change it so that the first character is in uppercase and then lowercase?

+4
source share
4 answers

You could write your own quite easily.

 public string Capitalise(string str) { if (String.IsNullOrEmpty(str)) return String.Empty; return Char.ToUpper(str[0]) + str.Substring(1).ToLower(); } 
+7
source

ToTitleCase() is the perfect solution. You can find the link to create an extension method below. Or for fun, you can create it yourself ...

 public string ToProperCase(string str) { if (string.IsNullOrEmpty(str)) return str; return str[0].ToUpper() + str.Substring(1).ToLower(); } // or an extension method public static string ToProperCase(this string str) { if (string.IsNullOrEmpty(str)) return str; return str[0].ToUpper() + str.Substring(1).ToLower(); } 

Link to create ToTitleCase() as an extension method on System.String:

http://geekswithblogs.net/mucman/archive/2007/03/26/109892.aspx

+8
source

Using

 CultureInfo.CurrentCulture.TextInfo.ToTitleCase( yourstring); 
+4
source

You can use the Title Case ... http://support.microsoft.com/kb/312890/EN-US/

+3
source

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


All Articles