简体   繁体   中英

How do I capitalized specific two letter word in a string in addition to the first letter of any word?

I need to capitalized the first letter of each word in a string and also capitalized specific words in the string if the length of the word is two. I can specify the two words in a <list> or Array .

Previous question and solution provided here capitalizes every word containing two letters. But this becomes a problem if someone name is only two letters. Example: Ja Rule or Robert Mo . I need to capitalized things like: NW SW MD if they appear in a string.

The following code capitalizes first letters of each word and both letters of words containing two characters. Again, this becomes a problem for two letter names:

var input = "dr. david BOWIE md";
TextInfo tCase = new CultureInfo("en-US", false).TextInfo;
var result =  tCase.ToTitleCase(input.ToLower());

result = string.Join(" ", result.Split(' ')
               .Select(i => i.Length == 2 ? i.ToUpperInvariant() : i));

Output:

Dr. David Bowie MD

Problem:

Jason De , also becomes Jason DE

Thanks.

Specify 2 letter words you want to capitalize (or 2 letter words you don't if list is shorter) and check if that word is contained in the list, if it is then capitalize whole word, otherwise leave it as title case

        var input = "dr. david BOWIE md";
        TextInfo tCase = new CultureInfo("en-US", false).TextInfo;
        var result = tCase.ToTitleCase(input.ToLower());

        var wordsToCapitalize = new []{"nw", "dw", "md"};

        result = string.Join(" ", result.Split(' ')
            .Select(i => (i.Length == 2 && wordsToCapitalize.Contains(i.ToLower())) ? i.ToUpperInvariant() : i));

        Assert.That(result, Is.EqualTo("Dr. David Bowie MD"));

You could also either title case inside the select, or have the words in the list title cased.

Here is the logic (as I cannot write c# very quickly and need to scoot off)

Split string into words.

Count length of words (4 for dr david bowie MD)

loop through each word until you get to last word in array ->

If length of array > 2 then last word - all capitals else - just capitalise as normal.

Might something as simple as this solve your problem?

var list = new List<string>{" of ", " is ", " an ", " to "};
var x = "This is an example of stuff to replace.";

foreach(var word in list){
    x = x.Replace(word, word.ToUpper());

    // Alternative if you don`t want to put spaces in the list:
    // x = x.Replace(" " + word + " ", " " + word.ToUpper() + " ");
}

The result will be: This IS AN example OF stuff TO replace.

"a computer is a stupid machine with the ability to do incredibly smart things", (Bill Bryson)

You need to give the computer the ability to distinguish between name and degree. Determining titles could happen by several approaches:

  • is part of a defined set of titles
  • is not part of a defined set of names
  • is at the end of the string (doesn't work for Jet Li)
  • at least two splits are left (doesn't work for Dr. Jet Mo Li)
  • it contains a vowel (doesn't work for Jet Li, if "Li" is a title)

If your are able to distinguish, then your shall give your program the same ability.

Be aware, that if cases exist where you just don't know if it is a name or a title then the computer cannot outsmart you. (Actually, there is the possibility to let your program scan a huge data source (ie the whole internet) and let it percept all possible titles and names and then let it decide if the "li" in "jet li" is a title or a name.)

Best regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM