Skip to content
Related Articles

Related Articles

Java String codePoint() Method with Examples

Improve Article
Save Article
  • Last Updated : 01 Feb, 2022
Improve Article
Save Article

A Java string consists of a group of characters and each character is associated with a Unicode point value (alias ASCII value). So to get the Unicode point value of a character in a string we will use the codepoint() method.

So in order to move further, we need to know what are the associated Unicode value of each character.

Letters

Unicode Values

Capital/Upper Case Letters (A-Z)

A-65, B-66, C-67,……., Y-89, Z-90

Small/Lower Case Letters(a-z) a-97, b-98, c-99,……., y-121, z-122

Not only the alphabets but also symbols, white-space, etc. are associated with some Unicode point value like for white-space it is 32.

There are 4 types of codepoint methods available. They are-

  1. codePointAt
  2. codePointBefore
  3. codePointCount
  4. offsetByCodePoints

Let’s discuss it one by one in detail along with syntax and examples.

1. codePointAt() Method

This method accepts an integer that specifies an index value in a string and returns an integer representing the Unicode point value for the character at the specified index in a string. If the index is not valid then it throws IndexOutOfBoundsException.

Syntax

stringVariable.codePointAt(index_value)

Example 1:

Java




// Java program to demonstrate
// the String codePointAt() Method
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        // initialization and declaration
        String str = "Geeks for Geeks";
       
        // finding codepoint value for character at index 1
        int e_codepoint = str.codePointAt(1);
        System.out.println(e_codepoint);
    }
}


Output

101

Explanation: At index 1 we have a letter ‘e’. So codePointAt() method returns unicode code point value for e.

Example 2:

Java




// Java program to demonstrate
// the String codePointAt() Method
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        // initialization and declaration
        String str = "Geeks for Geeks";
       
        // finding codepoint value for invalid index
        int e_codepoint = str.codePointAt(20);
        System.out.println(e_codepoint);
    }
}


Output

Explanation: Here the String size is 15 but we passed the index value as 20 which is beyond the size of the string. It will be considered as an invalid index and an error is thrown.

2. codePointBefore() Method

This method accepts an integer that specifies an index value in a string and returns an integer representing the Unicode point value for the character before at specified index in a string. If the index is not valid i.e., if the value passed is less than 1 or greater than String length then it throws IndexOutOfBoundsException.

Syntax

stringVariable.codePointBefore(index_value)

Code

Java




// Java program to demonstrate
// the String codePointBefore() Method
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        // initialization and declaration
        String str = "Geeks for Geeks";
 
        // finding codepoint value for character at index 0
        // index-1 (before)
        int G_codepoint = str.codePointBefore(1);
        System.out.println(G_codepoint);
    }
}


Output

71

Explanation: The index we passed is 1 but the method here is codePointBefore which finds the before character from the specified index which is G and its Unicode code point value- 71.

3. codePointCount() Method

This method accepts 2 parameters start_index and end_index and returns the number of Unicode code points present between two specified indexes. Here the start_index is inclusive and the end_index is exclusive. If any invalid index is passed as an argument then IndexOutOfBoundException occurs. 

Syntax

stringVariable.codePointCount(start_index, end_index)

Code

Java




// Java program to demonstrate
// the String codePointCount() Method
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        // initialization and declaration
        String str = "Geeks for Geeks";
       
        // finding  no of codepoint values
        // between specified indexes
        int codepoints_count = str.codePointCount(1, 5);
        System.out.println(codepoints_count);
    }
}


Output

4

Explanation: In between 1 and 5 index positions in a String str there are 4 letters- ‘e’, ‘e’, ‘k’, ‘s’. So it returns 4 as result. 

4. offsetByCodePoints() Method

This method accepts 2 arguments as parameters one is index value, and the other is offset_value. offsetByCodePoints returns an index value with in the string that is offset from the specified index by offset_value.

Syntax

stringVariable.offsetByCodePoints(index_value, offset_value)

Java




// Java program to demonstrate
// the String offsetCodePoints() Method
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        // initialization and declaration
        String str = "Geeks for Geeks";
       
        // finding  index in a string that is
        // offset from specified index
        int index_byOffset = str.offsetByCodePoints(1, 5);
        System.out.println(index_byOffset);
    }
}


Output

6

Explanation: In this example, offsetByCodePoints returns an index from specified index:1  by offset_value:5 i.e., 1+5=6.

These are all the methods that are related to the codePoint topic.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!