TextInfo.ToTitleCase 无法正常工作,有更好的解决方案吗?

发布于 2024-08-11 06:41:53 字数 474 浏览 2 评论 0原文

我正在尝试使我的文章标题格式良好,我目前正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明媚如初 2024-08-18 06:41:53

以下是来自我信任且自己使用过的来源的 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!

逆流 2024-08-18 06:41:53

我在提供的库中没有看到此问题的解决方案...它看起来是 扩展方法。有趣的是,它比单词列表稍微复杂一些,并且有一些变化。

芝加哥风格手册建议:

  1. 始终将第一个和后一个大写
    最后一个词。

  2. 所有名词、代词大写,
    形容词、动词、副词和
    从属连词(“as”,
    “because”、“although”)。

  3. 所有文章小写,坐标
    连词(“and”、“or”、“nor”)、
    和介词不管
    长度,当它们不是
    第一个或最后一个词。

  4. 不定式中的“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:

  1. Always capitalize the first and the
    last word.

  2. Capitalize all nouns, pronouns,
    adjectives, verbs, adverbs, and
    subordinate conjunctions ("as",
    "because", "although").

  3. Lowercase all articles, coordinate
    conjunctions ("and", "or", "nor"),
    and prepositions regardless of
    length, when they are other than the
    first or last word.

  4. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文