TextInfo.ToTitleCase 无法正常工作,有更好的解决方案吗?
我正在尝试使我的文章标题格式良好,我目前正在使用 TextInfo.ToTitleCase
进行格式化。它在大多数工作中都表现良好,但并不是那么完美。
例如:
- 原始字符串:战争与和平
- 预期结果:战争与和平
- 实际结果:战争与和平
微软也使用上面的例子,这显然是一个已知问题。我要做的就是手工为“a”、“and”、“or”等单词写一个列表(我不确定我是否可以获得完整的列表),这会是这个问题的最佳解决方案?
I'm trying to make my article title well-formatted, I'm currently using TextInfo.ToTitleCase
for formating. It does well on most of jobs, but it's not that perfect.
For example:
- Original String: war and peace
- Expected Result: War and Peace
- Actual Result: War And Peace
Microsoft also use above as the example, it's obviously a known problem. What I'm going to do is to write a list by hand for those words like "a", "and", "or" etc (I'm not sure I can get a complete list or not), would it be the best solution for this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是来自我信任且自己使用过的来源的 JavaScript 实现: http:// ejohn.org/blog/title-capitalization-in-javascript/
在源代码,他有一个仅小写的例外列表,您(我相信正确)认为您需要这些例外。
当然,工作是将其转换为 ASP.NET 可以在服务器端使用的东西,但逻辑已经经过了很多思考,这应该可以帮助您完成最终的任务。
祝你好运!
Here is a JavaScript implementation from a source I trust and have used myself: http://ejohn.org/blog/title-capitalization-in-javascript/
In the source code, he has a list of lowercase-only exceptions that you (I believe correctly) assumed you would need.
The work would be in converting it to something ASP.NET could use serverside, of course, but the logic has already got a lot of thought put into it which should help you with whatever you end up rolling.
Good luck!
我在提供的库中没有看到此问题的解决方案...它看起来是 扩展方法。有趣的是,它比单词列表稍微复杂一些,并且有一些变化。
芝加哥风格手册建议:
始终将第一个和后一个大写
最后一个词。
所有名词、代词大写,
形容词、动词、副词和
从属连词(“as”,
“because”、“although”)。
所有文章小写,坐标
连词(“and”、“or”、“nor”)、
和介词不管
长度,当它们不是
第一个或最后一个词。
不定式中的“to”小写。
最后一种情况似乎特别困难,因为您需要解析以确定“to”是否用于不定式中。
I've not seen a solution to this problem in a provided library... It looks a great candidate for an extension method. Interestingly, it's slightly more complex than just a list of words and has a few variations.
The Chicago Manual of Style suggests this:
Always capitalize the first and the
last word.
Capitalize all nouns, pronouns,
adjectives, verbs, adverbs, and
subordinate conjunctions ("as",
"because", "although").
Lowercase all articles, coordinate
conjunctions ("and", "or", "nor"),
and prepositions regardless of
length, when they are other than the
first or last word.
Lowercase the "to" in an infinitive.
The last case seems particular hard as you need to parse to determine if "to" is used in an infinitive.