New Tutorials:   NUMPY    TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL    Matplotlib    C++ Programs

Java Character isSurrogatePair() Method

Java isSurrogatePair() method is a part of Character class. This method is used to check whether the specified pair of char values is a valid Unicode surrogate pair or not.

It must be noted that this method is equivalent to the expression:

isHighSurrogate(high) && isLowSurrogate(low)

Syntax:

public static boolean isSurrogatePair(char high, char low)

Parameters:

The parameter passed are:

high - the high-surrogate code value to be checked

low - the low-surrogate code value to be checked

Returns:

Returns the boolean value true if the specified character pair represents a valid surrogate pair else return false.

Example 1:

Here, the character pairs are checked whether they are a valid surrogate pair or not.

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 is surrogate code unit??: false
? and8 is surrogate code unit??: false
? and? is surrogate code unit??: false
? and% is surrogate code unit?? : false
? and? is surrogate code unit??: true

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.

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!!");
		}
	}  
}


Enter the characters: y u
y and u is surrogate code unit?: false
*******************************************
Enter the characters: 1 2
1 and 2 is surrogate pair?: false

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.