How would you get an array of Unicode code points from a .NET String?

前端 未结 5 1411
日久生厌
日久生厌 2020-12-09 03:47

I have a list of character range restrictions that I need to check a string against, but the char type in .NET is UTF-16 and therefore some characters become wa

5条回答
  •  悲&欢浪女
    2020-12-09 04:29

    This solution produces the same results as the solution by Daniel A.A. Pelsmaeker but is a little bit shorter:

    public static int[] ToCodePoints(string s)
    {
        byte[] utf32bytes = Encoding.UTF32.GetBytes(s);
        int[] codepoints = new int[utf32bytes.Length / 4];
        Buffer.BlockCopy(utf32bytes, 0, codepoints, 0, utf32bytes.Length);
        return codepoints;
    }
    

提交回复
热议问题