The Java codePointAt method is one of the Java String Method which is used to return the Unicode of the Character at specified index position. In this article we will show you, How to use codePointAt method in Java Programming language with example.
TIP: The index position of the codePointAt Function will start from 0, Not 1.
Java codePointAt method syntax
The basic syntax of the codePointAt in Java Programming language is as shown below.
1 2 3 4 |
public int codePointAt(int Index_Position) //In order to use in program String_Object.codePointAt(int Index_Position) |
- String_Object: Please specify the valid String Object.
- Index_Position: Please specify the index position of a desired character.
TIP: Please refer ASCII Table to check the character codes of each and every character.
Return Value
The Java codePointAt Function will return the Unicode value of a Character from String_Object at specified Index_Position. If we specify the Index position out of range or negative value, then codePointAt Function will throw an error.
Java codePointAt Example
The Java string codePointAt method is used to return the Unicode value of a Character at specified index position. In this Java program, We are going to find the same.
JAVA CODE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package StringFunctions; public class CodePointAt { public static void main(String[] args) { String str = "Learn Free Java Tutorial"; int a = str.codePointAt(0); int b = str.codePointAt(8); int c = str.codePointAt(14); int d = str.codePointAt(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 8 = " + b); System.out.println( "Unicode Vlaue of Character at Index position 14 = " + c); System.out.println( "Unicode Vlaue of Character at Last Index position = " + d); } } |
OUTPUT
ANALYSIS
First we declared the String variable and assigned a value using following statement
1 |
String str = "Learn Free Java Tutorial"; |
Following statements will find the Character at index position 0, 8, 14 and then assign the Unicode values of those character to the integer variables a, b and c.
1 2 3 |
int a = str.codePointAt(0); int b = str.codePointAt(8); int c = str.codePointAt(14); |
If you observe the above screenshot, str.codePointAt(0) is returning 76. This is because, we all know that character at index position 0 is L and according to ASCII Table, the Unicode value of L is 76.
NOTE: You should count the space as One Character.
In the next line we are using the Java length() function to calculate the string length.
1 |
int d = str.codePointAt(str.length()- 1); |
From the above code snippet you can observe that, we are subtracting one from string length because, length of a string will count from 1 to n where as index position will start from 0 and end’s at n – 1.
Following System.out.println statements will print the output
1 2 3 4 |
System.out.println( "Unicode Vlaue of Character at Index position 0 = " + a); System.out.println( "Unicode Vlaue of Character at Index position 8 = " + b); System.out.println( "Unicode Vlaue of Character at Index position 14 = " + c); System.out.println( "Unicode Vlaue of Character at Last Index position = " + d); |
Thank You for Visiting Our Blog
Share your Feedback, or Code!!