How to capitalize the first character of each word, or the first character of a whole string, with C#?

前端 未结 9 1104
眼角桃花
眼角桃花 2020-11-29 05:27

I could write my own algorithm to do it, but I feel there should be the equivalent to ruby\'s humanize in C#.

I googled it but only found ways to humanize dates.

相关标签:
9条回答
  • 2020-11-29 06:03

    Far as I know, there's not a way to do that without writing (or cribbing) code. C# nets (ha!) you upper, lower and title (what you have) cases:

    http://support.microsoft.com/kb/312890/EN-US/

    0 讨论(0)
  • 2020-11-29 06:04

    There is no prebuilt solution for proper linguistic captialization in .NET. What kind of capitialization are you going for? Are you following the Chicago Manual of Style conventions? AMA or MLA? Even plain english sentence capitalization has 1000's of special exceptions for words. I can't speak to what ruby's humanize does, but I imagine it likely doesn't follow linguistic rules of capitalization and instead does something much simpler.

    Internally, we encountered this same issue and had to write a fairly large amount code just to handle proper (in our little world) casing of article titles, not even accounting for sentence capitalization. And it indeed does get "fuzzy" :)

    It really depends on what you need - why are you trying to convert the sentences to proper capitalization (and in what context)?

    0 讨论(0)
  • 2020-11-29 06:04

    I have achieved the same using custom extension methods. For First Letter of First sub-string use the method yourString.ToFirstLetterUpper(). For First Letter of Every sub-string excluding articles and some propositions, use the method yourString.ToAllFirstLetterInUpper(). Below is a console program:

    class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("this is my string".ToAllFirstLetterInUpper());
                Console.WriteLine("uniVersity of lonDon".ToAllFirstLetterInUpper());
            }
        }
    
        public static class StringExtension
        {
            public static string ToAllFirstLetterInUpper(this string str)
            {
                var array = str.Split(" ");
    
                for (int i = 0; i < array.Length; i++)
                {
                    if (array[i] == "" || array[i] == " " || listOfArticles_Prepositions().Contains(array[i])) continue;
                    array[i] = array[i].ToFirstLetterUpper();
                }
                return string.Join(" ", array);
            }
    
            private static string ToFirstLetterUpper(this string str)
            {
                return str?.First().ToString().ToUpper() + str?.Substring(1).ToLower();
            }
    
            private static string[] listOfArticles_Prepositions()
            {
                return new[]
                {
                    "in","on","to","of","and","or","for","a","an","is"
                };
            }
        }
    

    OUTPUT

    This is My String
    University of London
    Process finished with exit code 0.
    
    0 讨论(0)
  • 2020-11-29 06:06

    Use regular expressions for this looks much cleaner:

    string s = "the quick brown fox jumps over the lazy dog";
    s = Regex.Replace(s, @"(^\w)|(\s\w)", m => m.Value.ToUpper());
    
    0 讨论(0)
  • 2020-11-29 06:06

    CSS technique is ok but only changes the presentation of the string in the browser. A better method is to make the text itself capitalised before sending to browser.

    Most of the above implimentations are ok, but none of them address the issue of what happens if you have mixed case words that need to be preserved, or if you want to use true Title Case, for example:

    "Where to Study PHd Courses in the USA"

    or

    "IRS Form UB40a"

    Also using CultureInfo.CurrentCulture.TextInfo.ToTitleCase(string) preserves upper case words as in "sports and MLB baseball" which becomes "Sports And MLB Baseball" but if the whole string is put in upper case, then this causes an issue.

    So I put together a simple function that allows you to keep the capital and mixed case words and make small words lower case (if they are not at the start and end of the phrase) by including them in a specialCases and lowerCases string arrays:

    public static string TitleCase(string value) {
            string titleString = ""; // destination string, this will be returned by function
            if (!String.IsNullOrEmpty(value)) {
                string[] lowerCases = new string[12] { "of", "the", "in", "a", "an", "to", "and", "at", "from", "by", "on", "or"}; // list of lower case words that should only be capitalised at start and end of title
                string[] specialCases = new string[7] { "UK", "USA", "IRS", "UCLA", "PHd", "UB40a", "MSc" }; // list of words that need capitalisation preserved at any point in title
                string[] words = value.ToLower().Split(' ');
                bool wordAdded = false; // flag to confirm whether this word appears in special case list
                int counter = 1;
                foreach (string s in words) {
    
                    // check if word appears in lower case list
                    foreach (string lcWord in lowerCases) {
                        if (s.ToLower() == lcWord) {
                            // if lower case word is the first or last word of the title then it still needs capital so skip this bit.
                            if (counter == 0 || counter == words.Length) { break; };
                            titleString += lcWord;
                            wordAdded = true;
                            break;
                        }
                    }
    
                    // check if word appears in special case list
                    foreach (string scWord in specialCases) {
                        if (s.ToUpper() == scWord.ToUpper()) {
                            titleString += scWord;
                            wordAdded = true;
                            break;
                        }
                    }
    
                    if (!wordAdded) { // word does not appear in special cases or lower cases, so capitalise first letter and add to destination string
                        titleString += char.ToUpper(s[0]) + s.Substring(1).ToLower();
                    }
                    wordAdded = false;
    
                    if (counter < words.Length) {
                        titleString += " "; //dont forget to add spaces back in again!
                    }
                    counter++;
                }
            }
            return titleString;
        }
    

    This is just a quick and simple method - and can probably be improved a bit if you want to spend more time on it.

    if you want to keep the capitalisation of smaller words like "a" and "of" then just remove them from the special cases string array. Different organisations have different rules on capitalisation.

    You can see an example of this code in action on this site: Egg Donation London - this site automatically creates breadcrumb trails at the top of the pages by parsing the url eg "/services/uk-egg-bank/introduction" - then each folder name in the trail has hyphens replaced with spaces and capitalises the folder name, so uk-egg-bank becomes UK Egg Bank. (preserving the upper case 'UK')

    An extension of this code could be to have a lookup table of acronyms and uppercase/lowercase words in a shared text file, database table or web service so that the list of mixed case words can be maintained from one single place and apply to many different applications that rely on the function.

    0 讨论(0)
  • 2020-11-29 06:07

    All the examples seem to make the other characters lowered first which isn't what I needed.

    customerName = CustomerName <-- Which is what I wanted

    this is an example = This Is An Example

    public static string ToUpperEveryWord(this string s)
    {
        // Check for empty string.  
        if (string.IsNullOrEmpty(s))
        {
            return string.Empty;
        }
    
        var words = s.Split(' ');
    
        var t = "";
        foreach (var word in words)
        {
            t += char.ToUpper(word[0]) + word.Substring(1) + ' ';
        }
        return t.Trim();
    }
    
    0 讨论(0)
提交回复
热议问题