[英]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 Rule
或Robert 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:
确定标题的方法有几种:
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.