<p>Is there an easy way to capitalize the first letter of a string and lower the rest of it?  Is there a built in method or do I need to make my own?</p>

[{'answer_id': 72862, 'author': 'ageektrapped', 'author_id': 631, 'author_profile': 'https://Stackoverflow.com/users/631', 'pm_score': 9, 'selected': True, 'text': '<p><code>TextInfo.ToTitleCase()</code> capitalizes the first character in each token of a string.<br />\nIf there is no need to maintain Acronym Uppercasing, then you should include <code>ToLower()</code>.</p>\n\n<pre><code>string s = "JOHN DOE";\ns = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(s.ToLower());\n// Produces "John Doe"\n</code></pre>\n\n<p>If CurrentCulture is unavailable, use:</p>\n\n<pre><code>string s = "JOHN DOE";\ns = new System.Globalization.CultureInfo("en-US", false).TextInfo.ToTitleCase(s.ToLower());\n</code></pre>\n\n<p>See the <a href="http://msdn2.microsoft.com/en-us/library/system.globalization.textinfo.totitlecase.aspx" rel="noreferrer">MSDN Link</a> for a detailed description.</p>\n'}, {'answer_id': 72864, 'author': 'ckal', 'author_id': 10276, 'author_profile': 'https://Stackoverflow.com/users/10276', 'pm_score': 3, 'selected': False, 'text': '<p>ToTitleCase() should work for you.</p>\n\n<p><a href="http://support.microsoft.com/kb/312890" rel="noreferrer">http://support.microsoft.com/kb/312890</a></p>\n'}, {'answer_id': 72871, 'author': 'Nathan Baulch', 'author_id': 8799, 'author_profile': 'https://Stackoverflow.com/users/8799', 'pm_score': 7, 'selected': False, 'text': '<pre><code>CultureInfo.CurrentCulture.TextInfo.ToTitleCase("hello world");\n</code></pre>\n'}, {'answer_id': 72888, 'author': 'rjzii', 'author_id': 1185, 'author_profile': 'https://Stackoverflow.com/users/1185', 'pm_score': 2, 'selected': False, 'text': '<p>The most direct option is going to be to use the <a href="http://support.microsoft.com/kb/312890" rel="nofollow noreferrer">ToTitleCase</a> function that is available in .NET which should take care of the name most of the time. As <a href="https://stackoverflow.com/questions/72831/how-do-i-capitalize-first-letter-of-first-name-and-last-name-in-c#72850">edg</a> pointed out there are some names that it will not work for, but these are fairly rare so unless you are targeting a culture where such names are common it is not necessary something that you have to worry too much about.</p>\n\n<p>However if you are not working with a .NET langauge, then it depends on what the input looks like - if you have two separate fields for the first name and the last name then you can just capitalize the first letter lower the rest of it using substrings.</p>\n\n<pre><code>firstName = firstName.Substring(0, 1).ToUpper() + firstName.Substring(1).ToLower();\nlastName = lastName.Substring(0, 1).ToUpper() + lastName.Substring(1).ToLower();\n</code></pre>\n\n<p>However, if you are provided multiple names as part of the same string then you need to know how you are getting the information and <a href="http://msdn.microsoft.com/en-us/library/ms228388(VS.80).aspx" rel="nofollow noreferrer">split it</a> accordingly. So if you are getting a name like "John Doe" you an split the string based upon the space character. If it is in a format such as "Doe, John" you are going to need to split it based upon the comma. However, once you have it split apart you just apply the code shown previously.</p>\n'}, {'answer_id': 72901, 'author': 'David C', 'author_id': 12385, 'author_profile': 'https://Stackoverflow.com/users/12385', 'pm_score': 2, 'selected': False, 'text': '<p>CultureInfo.CurrentCulture.TextInfo.ToTitleCase ("my name");</p>\n\n<p>returns ~ My Name</p>\n\n<p>But the problem still exists with names like McFly as stated earlier.</p>\n'}, {'answer_id': 72910, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 1, 'selected': False, 'text': '<p>If your using vS2k8, you can use an extension method to add it to the String class:</p>\n\n<pre><code>public static string FirstLetterToUpper(this String input)\n{\n    return input = input.Substring(0, 1).ToUpper() + \n       input.Substring(1, input.Length - 1);\n}\n</code></pre>\n'}, {'answer_id': 72939, 'author': 'Michael Haren', 'author_id': 29, 'author_profile': 'https://Stackoverflow.com/users/29', 'pm_score': -1, 'selected': False, 'text': "<p>Like edg indicated, you'll need a more complex algorithm to handle special names (this is probably why many places force everything to upper case).</p>\n\n<p>Something like this untested c# should handle the simple case you requested:</p>\n\n<pre><code>public string SentenceCase(string input)\n{\n    return input(0, 1).ToUpper + input.Substring(1).ToLower;\n}\n</code></pre>\n"}, {'answer_id': 73688, 'author': 'Tundey', 'author_id': 1453, 'author_profile': 'https://Stackoverflow.com/users/1453', 'pm_score': 2, 'selected': False, 'text': "<p>The suggestions to use ToTitleCase won't work for strings that are all upper case. So you are gonna have to call ToUpper on the first char and ToLower on the remaining characters.</p>\n"}, {'answer_id': 73691, 'author': 'Jamie Ide', 'author_id': 12752, 'author_profile': 'https://Stackoverflow.com/users/12752', 'pm_score': 3, 'selected': False, 'text': '<p>Mc and Mac are common surname prefixes throughout the US, and there are others. TextInfo.ToTitleCase doesn\'t handle those cases and shouldn\'t be used for this purpose. Here\'s how I\'m doing it:</p>\n\n<pre><code>    public static string ToTitleCase(string str)\n    {\n        string result = str;\n        if (!string.IsNullOrEmpty(str))\n        {\n            var words = str.Split(\' \');\n            for (int index = 0; index &lt; words.Length; index++)\n            {\n                var s = words[index];\n                if (s.Length &gt; 0)\n                {\n                    words[index] = s[0].ToString().ToUpper() + s.Substring(1);\n                }\n            }\n            result = string.Join(" ", words);\n        }\n        return result;\n    }\n</code></pre>\n'}, {'answer_id': 74008, 'author': 'Eddie Velasquez', 'author_id': 12851, 'author_profile': 'https://Stackoverflow.com/users/12851', 'pm_score': 2, 'selected': False, 'text': '<p>This class does the trick. You can add new prefixes to the <strong>_prefixes</strong> static string array. </p>\n\n<pre><code>public static class StringExtensions\n{\n        public static string ToProperCase( this string original )\n        {\n            if( String.IsNullOrEmpty( original ) )\n                return original;\n\n            string result = _properNameRx.Replace( original.ToLower( CultureInfo.CurrentCulture ), HandleWord );\n            return result;\n        }\n\n        public static string WordToProperCase( this string word )\n        {\n            if( String.IsNullOrEmpty( word ) )\n                return word;\n\n            if( word.Length &gt; 1 )\n                return Char.ToUpper( word[0], CultureInfo.CurrentCulture ) + word.Substring( 1 );\n\n            return word.ToUpper( CultureInfo.CurrentCulture );\n        }\n\n        private static readonly Regex _properNameRx = new Regex( @"\\b(\\w+)\\b" );\n        private static readonly string[] _prefixes = {\n                                                         "mc"\n                                                     };\n\n        private static string HandleWord( Match m )\n        {\n            string word = m.Groups[1].Value;\n\n            foreach( string prefix in _prefixes )\n            {\n                if( word.StartsWith( prefix, StringComparison.CurrentCultureIgnoreCase ) )\n                    return prefix.WordToProperCase() + word.Substring( prefix.Length ).WordToProperCase();\n            }\n\n            return word.WordToProperCase();\n        }\n}\n</code></pre>\n'}, {'answer_id': 74023, 'author': 'Andy Rose', 'author_id': 1762, 'author_profile': 'https://Stackoverflow.com/users/1762', 'pm_score': 0, 'selected': False, 'text': '<p>To get round some of the issues/problems that have ben highlighted I would suggest converting the string to lower case first and then call the ToTitleCase method. You could then use IndexOf(" Mc") or IndexOf(" O\\\'") to determine special cases that need more specific attention.</p>\n\n<pre><code>inputString = inputString.ToLower();\ninputString = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(inputString);\nint indexOfMc = inputString.IndexOf(" Mc");\nif(indexOfMc  &gt; 0)\n{\n   inputString.Substring(0, indexOfMc + 3) + inputString[indexOfMc + 3].ToString().ToUpper() + inputString.Substring(indexOfMc + 4);\n}\n</code></pre>\n'}, {'answer_id': 3169381, 'author': 'Ganesan SubbiahPandian', 'author_id': 382422, 'author_profile': 'https://Stackoverflow.com/users/382422', 'pm_score': 5, 'selected': False, 'text': '<pre><code>String test = "HELLO HOW ARE YOU";\nstring s = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(test);\n</code></pre>\n\n<p>The above code wont work .....</p>\n\n<p>so put the below code  by convert to lower then apply the function</p>\n\n<pre><code>String test = "HELLO HOW ARE YOU";\nstring s = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(test.ToLower());\n</code></pre>\n'}, {'answer_id': 4759134, 'author': 'Ton Snoei', 'author_id': 584472, 'author_profile': 'https://Stackoverflow.com/users/584472', 'pm_score': 2, 'selected': False, 'text': '<p>I use my own method to get this fixed:</p>\n\n<p>For example the phrase: "hello world. hello this is the stackoverflow world." will be "Hello World. Hello This Is The Stackoverflow World.". Regex \\b (start of a word) \\w (first charactor of the word) will do the trick. </p>\n\n<pre><code>/// &lt;summary&gt;\n/// Makes each first letter of a word uppercase. The rest will be lowercase\n/// &lt;/summary&gt;\n/// &lt;param name="Phrase"&gt;&lt;/param&gt;\n/// &lt;returns&gt;&lt;/returns&gt;\npublic static string FormatWordsWithFirstCapital(string Phrase)\n{\n     MatchCollection Matches = Regex.Matches(Phrase, "\\\\b\\\\w");\n     Phrase = Phrase.ToLower();\n     foreach (Match Match in Matches)\n         Phrase = Phrase.Remove(Match.Index, 1).Insert(Match.Index, Match.Value.ToUpper());\n\n     return Phrase;\n}\n</code></pre>\n'}, {'answer_id': 12413336, 'author': 'TrentVB', 'author_id': 1118056, 'author_profile': 'https://Stackoverflow.com/users/1118056', 'pm_score': 0, 'selected': False, 'text': '<p>I like this way:</p>\n\n<pre><code>using System.Globalization;\n...\nTextInfo myTi = new CultureInfo("en-Us",false).TextInfo;\nstring raw = "THIS IS ALL CAPS";\nstring firstCapOnly = myTi.ToTitleCase(raw.ToLower());\n</code></pre>\n\n<p>Lifted from this <a href="http://msdn.microsoft.com/en-us/library/system.globalization.textinfo.totitlecase%28v=vs.100%29.aspx" rel="nofollow">MSDN article</a>.</p>\n'}, {'answer_id': 14121370, 'author': 'Arun', 'author_id': 1063254, 'author_profile': 'https://Stackoverflow.com/users/1063254', 'pm_score': 0, 'selected': False, 'text': '<p>Hope this helps you.</p>\n\n<pre><code>String fName = "firstname";\nString lName = "lastname";\nString capitalizedFName = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(fName);\nString capitalizedLName = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(lName);\n</code></pre>\n'}, {'answer_id': 19517670, 'author': 'polkduran', 'author_id': 848634, 'author_profile': 'https://Stackoverflow.com/users/848634', 'pm_score': 4, 'selected': False, 'text': '<p>There are some cases that <code>CultureInfo.CurrentCulture.TextInfo.ToTitleCase</code> cannot handle, for example : the apostrophe <code>\'</code>.</p>\n\n<pre><code>string input = CultureInfo.CurrentCulture.TextInfo.ToTitleCase("o\'reilly, m\'grego, d\'angelo");\n// input = O\'reilly, M\'grego, D\'angelo\n</code></pre>\n\n<p>A <strong>regex</strong> can also be used <code>\\b[a-zA-Z]</code> to identify the starting character of a word after a word boundary <code>\\b</code>, then we need just to replace the match by its upper case equivalence thanks to the <a href="http://msdn.microsoft.com/fr-fr/library/vstudio/ht1sxswy%28v=vs.110%29.aspx"><code>Regex.Replace(string input,string pattern,MatchEvaluator evaluator)</code></a> method : </p>\n\n<pre><code>string input = "o\'reilly, m\'grego, d\'angelo";\ninput = Regex.Replace(input.ToLower(), @"\\b[a-zA-Z]", m =&gt; m.Value.ToUpper());\n// input = O\'Reilly, M\'Grego, D\'Angelo\n</code></pre>\n\n<p>The <strong>regex</strong> can be tuned if needed, for instance, if we want to handle the <code>MacDonald</code> and <code>McFry</code> cases the regex becomes : <code>(?&lt;=\\b(?:mc|mac)?)[a-zA-Z]</code></p>\n\n<pre><code>string input = "o\'reilly, m\'grego, d\'angelo, macdonald\'s, mcfry";\ninput = Regex.Replace(input.ToLower(), @"(?&lt;=\\b(?:mc|mac)?)[a-zA-Z]", m =&gt; m.Value.ToUpper());\n// input = O\'Reilly, M\'Grego, D\'Angelo, MacDonald\'S, McFry\n</code></pre>\n\n<p>If we need to handle more prefixes we only need to modify the group <code>(?:mc|mac)</code>, for example to add french prefixes <code>du, de</code> : <code>(?:mc|mac|du|de)</code>.</p>\n\n<p>Finally, we can realize that this <strong>regex</strong> will also match the case <code>MacDonald\'S</code> for the last <code>\'s</code> so we need to handle it in the <strong>regex</strong> with a negative look behind <code>(?&lt;!\'s\\b)</code>. At the end we have :</p>\n\n<pre><code>string input = "o\'reilly, m\'grego, d\'angelo, macdonald\'s, mcfry";\ninput = Regex.Replace(input.ToLower(), @"(?&lt;=\\b(?:mc|mac)?)[a-zA-Z](?&lt;!\'s\\b)", m =&gt; m.Value.ToUpper());\n// input = O\'Reilly, M\'Grego, D\'Angelo, MacDonald\'s, McFry\n</code></pre>\n'}, {'answer_id': 40882007, 'author': 'Govind Singh Rawat', 'author_id': 5580191, 'author_profile': 'https://Stackoverflow.com/users/5580191', 'pm_score': 0, 'selected': False, 'text': '<pre><code> public static string ConvertToCaptilize(string input)\n        {\n            if (!string.IsNullOrEmpty(input))\n            {\n                string[] arrUserInput = input.Split(\' \');\n\n\n                // Initialize a string builder object for the output\n                StringBuilder sbOutPut = new StringBuilder();\n\n\n                // Loop thru each character in the string array\n                foreach (string str in arrUserInput)\n                {\n                    if (!string.IsNullOrEmpty(str))\n                    {\n                        var charArray = str.ToCharArray();\n                        int k = 0;\n                        foreach (var cr in charArray)\n                        {\n                            char c;\n                            c = k == 0 ? char.ToUpper(cr) : char.ToLower(cr);\n                            sbOutPut.Append(c);\n                            k++;\n                        }\n\n\n                    }\n                    sbOutPut.Append(" ");\n                }\n                return sbOutPut.ToString();\n            }\n            return string.Empty;\n\n        }\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/72831', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/9938/']