代码之家  ›  专栏  ›  技术社区  ›  user2696668

Java字符。C中的字符数#

  •  3
  • user2696668  · 技术社区  · 7 年前

    我编写了java代码来获取位置0处的字符串代码点,然后检查表示该代码点所需的字符数。我正在寻找等效的c#方法,该方法将输入作为代码点,并返回表示代码点所需的字符计数

     Below is the **Java** code
     final int cp = str.codePointAt(0);
     int count = Character.charCount(cp);
    

    寻找等效的C#代码

     int cp  =  char.ConvertToUtf32(input, 0);
     int count = ????
    
    1 回复  |  直到 7 年前
        1
  •  4
  •   Sweeper    7 年前

    来自的文档 charCount ,

    确定表示指定字符(Unicode代码点)所需的字符值数。如果指定的字符等于或大于0x10000,则该方法返回2。否则,该方法返回1。

    所以你可以自己写这样一个方法!

    public static int CharCount(int codePoint) {
        return codePoint >= 0x10000 ? 2 : 1;
    }
    

    或者使用新的表达式bodied members语法,

    public static int CharCount(int codePoint) =>
        codePoint >= 0x10000 ? 2 : 1;