Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

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

I'm writing a slug generator for making nice urls. I'd like to convert m2 to m2, but in a generic way that does this for all superscript (or subscript), not just a simple replace statement.

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...