Java codePointBefore Method

The Java codePointBefore method is one of the String Methods, which is to return the Unicode of the Character before the specified index position. In this article, we will show how to use codePointBefore method in Java Programming language with an example. The index position will start from 0, Not 1.

codePointBefore Syntax

The basic syntax of the codePointBefore in Java Programming language is as shown below.

public int codePointBefore(int Index_Position) 

//In order to use in program
String_Object.codePointBefore(int Index_Position)
  • String_Object: Please specify the valid String Object.
  • Index_Position: Please specify the index position of the desired character.

This Function will return the Unicode value of a Character from String_Object at specified Index_Position minus 1. For example, if we specify codePointBefore(5), it will return the Unicode value of the character at index position 4. If we specify the Index position out of range or negative value, then the Function will throw an error.

Java codePointBefore Example

In this program, We are going to use the string codePointBefore method to return the Unicode value of a Character at a specified index position minus 1.

package StringFunctions;

public class CodePointBefore {
	public static void main(String[] args) {
		String str = "Learn Free Java Tutorial";
		
		int a = str.codePointBefore(1);
		int b = str.codePointBefore(11);
		int c = str.codePointBefore(22);
		int d = str.codePointBefore(str.length()- 1);
		
		System.out.println( "Unicode Vlaue of Character at Index position 0 = " + a);
		System.out.println( "Unicode Vlaue of Character at Index position 10 = " + b);
		System.out.println( "Unicode Vlaue of Character at Index position 21 = " + c);
		System.out.println( "Unicode Vlaue of Character at Last Index position - 1 = " + d);
	}
}
Java codePointBefore Method 1

Within this Java codePointBefore method example, we declared the String variable and assigned a value.

The following codePointBefore String Method statements will find the Character at index position 0, 10, 21. Then assign the Unicode values of those characters to the integer variables a, b, and c.

int a = str.codePointBefore(1);
int b = str.codePointBefore(11);
int c = str.codePointBefore(22);

If you observe the above screenshot, str.codePointAt(11) is returning 32. It is because we all know that character at index position 10 is Empty Space, and according to ASCII Table, the Unicode value of Empty Space is 32. You should count the space as One Character. Please refer ASCII Table to check the character codes of every character.

In the next line, we are using the length function along with Java codePointBefore to calculate the string length.

int d = str.codePointBefore(str.length()- 1);

From the above code snippet, we are subtracting one from string length. Because the length of a string will count from 1 to n where the index position will start from 0 and end’s at n – 1. The following four Java System.out.println statements will print the output.