user2696668
user2696668

Reputation: 165

Java Character.charCount in C#

I have java code written to get string code point at location 0 and then to check the number of characters required to represent that code point . I am looking for equivalent c# method which shall take input as code point and return the character count needed to represent the code point

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

Looking for Equivalent C# code

 int cp  =  char.ConvertToUtf32(input, 0);
 int count = ????

Upvotes: 3

Views: 431

Answers (1)

Sweeper
Sweeper

Reputation: 273700

From the docs of charCount,

Determines the number of char values needed to represent the specified character (Unicode code point). If the specified character is equal to or greater than 0x10000, then the method returns 2. Otherwise, the method returns 1.

So you can write such a method yourself!

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

Or using the new expression bodied members syntax,

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

Upvotes: 4

Related Questions