How to convert super- or subscript to normal text in C#

六眼飞鱼酱① 提交于 2019-12-18 05:55:32

问题


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?


回答1:


Thanks Johannes, you set me on the right track. The code with which I got it to work looks as follows:

public string ConvertSuperscript(string value)
{
    string stringFormKd = value.Normalize(NormalizationForm.FormKD);
    StringBuilder stringBuilder = new StringBuilder();

    foreach (char character in stringFormKd)
    {
        UnicodeCategory unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(character);
        if (unicodeCategory != UnicodeCategory.NonSpacingMark)
        {
            stringBuilder.Append(character);
        }
    }

    return stringBuilder.ToString().Normalize(NormalizationForm.FormKC);
}

I tried the canonical decomposition before, but it needed the compatibility decomposition to work properly.




回答2:


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.



来源:https://stackoverflow.com/questions/2673513/how-to-convert-super-or-subscript-to-normal-text-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!