如何在 C# 中将上标或下标转换为普通文本
我正在编写一个 slug 生成器来制作漂亮的网址。我想将 m² 转换为 m2,但以通用方式对所有上标(或下标)执行此操作,而不仅仅是简单的替换语句。
有什么想法吗?
I'm writing a slug generator for making nice urls. I'd like to convert m² to m2, but in a generic way that does this for all superscript (or subscript), not just a simple replace statement.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的字符串出现在 URL 中,那么我假设它是某种采用 unicode 字符形式的常规非格式化文本(而不是 MS Word 文档)。在 unicode 中,只能将某些字符作为上标或下标。它们并不多,一个简单的 switch 语句就可以完成这项工作。
如果您尝试将可能包含各种字符的格式化文本转换为上标或下标,这意味着它们不会直接表示为 unicode,并且很大程度上取决于文本的格式。如果是这样,请在问题中提供更多信息。
If your string is going in the URL, then I assume it is some kind of regular non-formatted text in the form of unicode characters (as opposed to an MS Word doc for example). In unicode, you can only have certain characters as superscript or subscript. They are not that many and a simple switch statement would do the job.
If you are trying to convert formatted text that could contain all kinds of characters as superscript or subscript, that means they are not directly represented as unicode, and it would depend a lot on the format of the text. If so, please give more information in the question.
谢谢约翰内斯,你让我走上了正轨。我让它工作的代码如下所示:
我之前尝试过规范分解,但它需要兼容性分解才能正常工作。
Thanks Johannes, you set me on the right track. The code with which I got it to work looks as follows:
I tried the canonical decomposition before, but it needed the compatibility decomposition to work properly.