How to capitalize the first character of each word

2019-01-09 00:30发布

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.

Examples:

  • A way to turn "Lorem Lipsum Et" into "Lorem lipsum et"
  • A way to turn "Lorem lipsum et" into "Lorem Lipsum Et"

7条回答
▲ chillily
2楼-- · 2019-01-09 00:42

There is another elegant solution :

Define the function ToTitleCase in an static class of your projet

using System.Globalization;

public static string ToTitleCase(this string title)
{
    return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(title.ToLower()); 
}

And then use it like a string extension anywhere on your project:

"have a good day !".ToTitleCase() // "Have A Good Day !"
查看更多
三岁会撩人
3楼-- · 2019-01-09 00:46

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)?

查看更多
放我归山
4楼-- · 2019-01-09 00:47

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());
查看更多
smile是对你的礼貌
5楼-- · 2019-01-09 00:50

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/

查看更多
聊天终结者
6楼-- · 2019-01-09 00:53

If you just want to capitalize the first character, just stick this in a utility method of your own:

return string.IsNullOrEmpty(str) 
    ? str
    : str[0].ToUpperInvariant() + str.Substring(1).ToLowerInvariant();

There's also a library method to capitalize the first character of every word:

http://msdn.microsoft.com/en-us/library/system.globalization.textinfo.totitlecase.aspx

查看更多
Fickle 薄情
7楼-- · 2019-01-09 00:53

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.

查看更多
登录 后发表回答