简体   繁体   English

除了任何单词的第一个字母外,如何在字符串中大写特定的两个字母的单词?

[英]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. 我需要将字符串中每个单词的首字母大写,如果单词的长度为两个,还需要将字符串中的specific单词大写。 I can specify the two words in a <list> or Array . 我可以在<list>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 . 例如: Ja RuleRobert Mo I need to capitalized things like: NW SW MD if they appear in a string. 我需要使用大写形式,例如: NW SW MD如果它们出现在字符串中。

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 Jason De ,也成为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 指定要大写的2个字母词(如果列表较短,则不要输入2个字母词),并检查该单词是否包含在列表中(如果是大写的话),否则将其保留为标题大小写

        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. 您也可以在select内添加标题大小写,也可以在列表标题中添加单词大小写。

Here is the logic (as I cannot write c# very quickly and need to scoot off) 这是逻辑(因为我无法很快地编写C#,因此需要深入研究)

Split string into words. 将字符串拆分为单词。

Count length of words (4 for dr david bowie MD) 计算单词的长度(大卫·鲍伊博士MD为4)

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. 如果数组的长度> 2,则最后一个单词-其他所有大写字母-照常大写即可。

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. 结果将是: This IS AN example OF stuff TO replace.

"a computer is a stupid machine with the ability to do incredibly smart things", (Bill Bryson) “计算机是一台愚蠢的机器,能够执行令人难以置信的智能事情”,(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) 至少剩下两个分割(对Jet Mo Li博士无效)
  • it contains a vowel (doesn't work for Jet Li, if "Li" is a title) 它包含一个元音(如果“ Li”是标题,则不适用于Jet Li)

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.) (实际上,有可能让您的程序扫描一个巨大的数据源(即整个互联网),并使其感知所有可能的标题和名称,然后让它决定“ jet li”中的“ li”是标题还是一个名字。)

Best regards 最好的祝福

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 除了任何单词的第一个字母,我如何将字符串中的两个字母的单词都大写? - How do I capitalized any two letter word in a string in addition to the first letter of any word? 如何替换字符串中每个单词的第一个字母? - How do i replace first letter in each word in a string? 验证用户仅输入一个单词的答案,并且首字母大写 - Validating user only enters a one word answer and that the first letter is capitalized 如何检查字符串是否包含以第一个字母开头的动态单词 - How to check if the string contains dynamic word that starts with first letter 查找字符串中带有特定字母的所有单词 - Finding all word with a specific letter in a string 如何解决:字符串中每个单词的大写首字母-一个字母单词除外 - How to Fix: Uppercase first letter of every word in the string - except for one letter words 每个超过 3 个字母的单词都以大写开头 + 第一行是大写 - 快速问题 - Every word longer than 3 letter starts with capitalized + first line is Upper case - quick question 如何检查字符串的第一个字符,如果是字母,C# 中的任何字母 - How to check first character of a string if a letter, any letter in C# 如何获取句子中特定(第n个)单词的第一个字母的索引 - how to get index of first letter of specific (n-th) word in sentence 你如何抓住字符串中的第一个字母? - How do you grab the first letter in a string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM