Java Character.isSurrogatePair()
方法
原文:https://www.studytonight.com/java-wrapper-class/java-character-issurrogatepair-method
Java isSurrogatePair()
方法是Character
类的一部分。此方法用于检查指定的字符值对是否是有效的 Unicode 代理项对。
必须注意的是,该方法等效于表达式:
isHighSurrogate(high) && isLowSurrogate(low)
语法:
public static boolean isSurrogatePair(char high, char low)
参数:
传递参数有:
高 -要检查的高替代代码值
低 -要检查的低替代代码值
返回:
如果指定的字符对代表有效的代理对,则返回布尔值true
,否则返回false
。
例 1:
这里,检查字符对是否是有效的代理对。
public class StudyTonight
{
public static void main(String[] args)
{
char ch1 = '\udc00';
char ch2 = 'c';
char ch3 = '8';
char ch4 = '\udbff';
char ch5 = '%';
boolean b1 = Character.isSurrogatePair(ch1,ch2);
boolean b2 = Character.isSurrogatePair(ch1,ch3);
boolean b3 = Character.isSurrogatePair(ch1,ch4);
boolean b4 = Character.isSurrogatePair(ch1,ch5);
boolean b5 = Character.isSurrogatePair(ch4,ch1);
System.out.println(ch1+" and " +ch2 +" is surrogate code unit??: "+b1);
System.out.println(ch1+" and " +ch3 +" is surrogate code unit??: "+b2);
System.out.println(ch1+" and " +ch4 +" is surrogate code unit??: "+b3);
System.out.println(ch1+" and " +ch5 +" is surrogate code unit?? : "+b4);
System.out.println(ch4+" and " +ch1 +" is surrogate code unit??: "+b5);
}
}
?andc 是代理代码单元??:假 ?8 是代理代码单位??:假 ?然后呢。是代理代码单元??:假 ?而%是代理代码单位??:假 ?然后呢。是代理代码单元??:真
例 2:
这里有一个用户定义的例子,任何使用这段代码的人都可以输入自己选择的值,并获得等效的输出。
import java.util.Scanner;
public class StudyTonight
{
public static void main(String[] args)
{
try
{
System.out.print("Enter the characters: ");
Scanner sc = new Scanner(System.in);
char ch1 = sc.next().charAt(0);
char ch2 = sc.next().charAt(0);
boolean b = Character.isSurrogatePair(ch1,ch2);
System.out.println(ch1+ " and " +ch2 + " is surrogate pair?: "+b);
}
catch(Exception e)
{
System.out.println("Invalid Input!!");
}
}
}
输入字符:y u y 和 u 是代理代码单位?:假
- *输入字符:1 2 1 和 2 是代孕对?:假
实时示例:
在这里,您可以测试实时代码示例。您可以为不同的值执行示例,甚至可以编辑和编写您的示例来测试 Java 代码。