Java Character.toCodePoint()
方法
原文:https://www.studytonight.com/java-wrapper-class/java-character-tocodepointchar-high-char-low-method
Java toCodePoint()
方法是Character
类的一部分。此方法将指定的代理项对转换为其补充代码点值。
必须注意,此方法不验证指定的代理项对。如有必要,呼叫者必须使用isSurrogatePair()
进行验证。
语法:
public static int toCodePoint(char high, char low)
参数:
传递参数有:
high
-高代理代码单元low
-低代理代码单元
返回:
返回由指定代理项对组成的补充代码点。
例 1:
这里,返回指定代理项对的补充代码点。
public class StudyTonight
{
public static void main(String[] args)
{
char h1 = '\ud880';
char l1 = '\ud110';
char h2 = 'I';
char l2 = 'O';
int r1 = Character.toCodePoint(h1, l1);
int r2 = Character.toCodePoint(h2, l2);
System.out.println("The result for the above character is given as : "+r1);
System.out.println("The result for the above character is given as : "+r2);
}
}
上述字符的结果为:193808 上述字符的结果为:-56539057
例 2:
这里有一个用户定义的例子,任何使用这段代码的人都可以输入自己选择的值,并获得等效的输出。
import java.util.Scanner;
public class StudyTonight
{
public static void main(String[] args)
{
try
{
System.out.print("Enter the character: ");
Scanner sc = new Scanner(System.in);
char high = sc.next().charAt(0);
char low = sc.next().charAt(0);
int r = Character.toCodePoint(high, low);
System.out.println("The result for the given pair is : "+r);
}
catch(Exception e)
{
System.out.println("Invalid Input!!");
}
}
}
输入字符:G y 给定对的结果为:-56541063
- *给定对的结果为:-56556487
实时示例:
在这里,您可以测试实时代码示例。您可以为不同的值执行示例,甚至可以编辑和编写您的示例来测试 Java 代码。