使用 C# 将字符串中的单词大写
我需要一个字符串,并将其中的单词大写。某些单词(“in”、“at”等)不是大写的,如果遇到,则会更改为小写。第一个单词应始终大写。像“McFly”这样的姓氏不在当前范围内,因此相同的规则将适用于它们 - 仅首字母大写。
例如:“of mouse and men By CNN”应改为“Of Mice and Men by CNN”。 (因此 ToTitleString 在这里不起作用。)
最好的方法是什么?
我想用空格分割字符串,然后检查每个单词,必要时进行更改,然后将其连接到前一个单词,等等。 这看起来很天真,我想知道是否有更好的方法来做到这一点。我正在使用.NET 3.5。
I need to take a string, and capitalize words in it. Certain words ("in", "at", etc.), are not capitalized and are changed to lower case if encountered. The first word should always be capitalized. Last names like "McFly" are not in the current scope, so the same rule will apply to them - only first letter capitalized.
For example: "of mice and men By CNN" should be changed to "Of Mice and Men by CNN". (Therefore ToTitleString won't work here.)
What would be the best way to do that?
I thought of splitting the string by spaces, and go over each word, changing it if necessary, and concatenating it to the previous word, and so on.
It seems pretty naive and I was wondering if there's a better way to do it. I am using .NET 3.5.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您应该像您所描述的那样创建自己的函数。
You should create your own function like you're describing.
最简单明显的解决方案(对于英语句子)是:
"sentence".Split(" ")
空格字符上的句子item [i][0].ToUpper()
,The easiest obvious solution (for English sentences) would be to:
"sentence".Split(" ")
the sentence on space charactersitem[i][0].ToUpper()
,处理简单情况的一种不聪明的方法:
A non-clever approach that handles the simple case:
您可以拥有一个包含您想要忽略的单词的字典,将句子分成短语(.split(' ')),并对于每个短语,检查该短语是否存在于字典中,如果不存在,则将第一个字符大写然后,将字符串添加到字符串缓冲区。如果您当前正在处理的短语在字典中,只需将其添加到字符串缓冲区即可。
You can have a Dictionary having the words you would like to ignore, split the sentence in phrases (.split(' ')) and for each phrase, check if the phrase exists in the dictionary, if it does not, capitalize the first character and then, add the string to a string buffer. If the phrase you are currently processing is in the dictionary, simply add it to the string buffer.
对 jonnii 的答案略有改进:
A slight improvement on jonnii's answer:
尝试这样的操作:
如果您需要处理复杂的姓氏,您可以稍后更新
CapitalizeWord
方法。将这些方法添加到类中并像这样使用它:
Try something like this:
You can then later update the
CapitalizeWord
method if you need to handle complex surnames.Add those methods to a class and use it like this:
首先使用
ToTitleCase()
,然后保留适用单词的列表,然后Replace
回这些适用单词的全小写版本(前提是列表很小)。适用单词的列表可以保存在字典中并非常有效地循环,用
.ToLower()
等效项替换。Use
ToTitleCase()
first and then keep a list of applicable words andReplace
back to the all-lower-case version of those applicable words (provided that list is small).The list of applicable words could be kept in a dictionary and looped through pretty efficiently, replacing with the
.ToLower()
equivalent.另一个问题的答案,如何将名称大写< /em> -
An answer from another question, How to Capitalize names -
根据您计划进行大写的频率,我会采用简单的方法。您可以使用正则表达式来完成此操作,但您不希望某些单词大写这一事实使得这有点棘手。
您可以使用正则表达式通过两次传递来完成此操作:
此输出
Of Mice and Men isn't by CNN
。第一个表达式将单词边界上的每个字母大写,第二个表达式将与列表匹配且由空格包围的所有单词小写。
这种方法的缺点是您正在使用正则表达式(现在您有两个问题)并且您需要保持排除的单词列表是最新的。我的 regex-fu 不够好,无法用一个表达式来完成它,但它可能是可能的。
Depending on how often you plan on doing the capitalization I'd go with the naive approach. You could possibly do it with a regular expression, but the fact that you don't want certain words capitalized makes that a little trickier.
You can do it with two passes using regular expressions:
This outputs
Of Mice and Men Isn't by CNN
.The first expression capitalizes every letter on a word boundary and the second one downcases any words matching the list that are surrounded by white space.
The downsides to this approach is that you're using regexs (now you have two problems) and you'll need to keep that list of excluded words up to date. My regex-fu isn't good enough to be able to do it in one expression, but it might be possible.
使用
转换为正确的大小写,然后您可以按照您提到的那样循环遍历关键字。
Use
to convert to proper case and then you can loop through the keywords as you have mentioned.