Java String CodePoints() Method

Java String CodePoints | This blog is mainly focused on the java built-in method codePoints(), codePointAt(), codePointBefore(), codePointCount(), and offsetByCodePoints(). As we all know each character in a programming language has been assigned an ASCII (American Standard Code for Information Exchange) value or Unicode value. In order to fetch those values of the particular character, we can use the codepoints method.

There are different Unicode for small letters and capital letters now, see the Unicode values for both small letters and capital alphabets:-
A-Z= 65 – 90
a-z= 97 – 122

These Unicode values are a series of numbers where the capital letters start from 65 and end at 90 that is ‘A’ has a Unicode value of 65 and ‘Z’ has 90, the same way ‘a’ starts from 97 and ‘z’ ends ar 122.

Below are the different variations in the codepoints method:-

1. public IntStream codePoints():- Returns a stream of code point values from this sequence.
2. public int codePointAt(int index):- Returns the character (Unicode code point) at the specified index.
3. public int codePointBefore(int index):- Returns the character (Unicode code point) before the specified index.
4. public int codePointCount(int beginIndex, int endIndex):- Returns the number of Unicode code points in the specified text range of this String.
5. public int offsetByCodePoints(int index, int codePointOffset):- Returns the index within this String that is offset from the given index by codePointOffset code points.

Java String codePoints() Method Example

The Java String class codePoints() method returns a stream of code point values from this sequence. Any surrogate pairs encountered in the sequence are combined as if by Character.toCodePoint() and the result is passed to the stream.

import java.util.stream.IntStream;

public class Main {
   public static void main(String[] args) {
      String string = "Know Program";
      System.out.println("Input string value : " + string);
      IntStream intStream = string.codePoints();
      System.out.println("Printing each char from the string as an ASCII value: ");
      intStream.forEach(value -> System.out.print(value + " "));
   }
}

Output:-

Input string value: Know Program
Printing each char from the string as an ASCII value:
75 110 111 119 32 80 114 111 103 114 97 109

Program to Remove Specific Character From String

To remove a specific character from the given string we can apply the filter() method on the resultant IntStream. Let us see it throw an example:-

import java.util.stream.IntStream;

public class Main {
   public static void main(String[] args) {
      String string = "Know Program";
      System.out.println("Input string value: " + string);
      IntStream intStream = string.codePoints();
      int character = 'o';
      String newString = intStream.filter(ch -> ch != character)
            .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
            .toString();
      System.out.println("New String: " + newString);
   }
}

Output:-

Input string value: Know Program
New String: Knw Prgram

Java String codePointAt() Method Example

The codePointAt() method of the String class returns the character (Unicode code point) at the specified index. Let us see it through some examples:-

public class Main {
   public static void main(String[] args) {
      String string = "Know Program";
      int value = string.codePointAt(1);
      System.out.println(value);
   }
}

Output:-

110

public class Main {
   public static void main(String[] args) {
      String string = "Better Late than Never...";
      int string1 = string.codePointAt(20);
      System.out.println(string1);
   }
}

Output:-

101

In the above example, if we pass an int value greater than the length of the string then we will get StringIndexOutOfBoundsException.

int string1 = string.codePointAt(100);

Exception in thread “main” java.lang.StringIndexOutOfBoundsException: index 100, length 25

Java String codePointBefore() Method Example

The codePointBefore() method of the String class returns the character (Unicode code point) before the specified index. Let us see it through an example:-

public class Main {
   public static void main(String[] args) {
      String string = "Actions speak louder than words";
      int string1 = string.codePointBefore(1);
      System.out.println(string1);
      int string2 = string.codePointBefore(5);
      System.out.println(string2);
   }
}

Output:-

65
111

Java String codePointCount() Method Example

The codePointCount() method of the Java String class returns the number of Unicode code points in the specified text range of this String.

public class Main {
   public static void main(String[] args) {
      String string = "Actions speak louder than words";
      int value1 = string.codePointCount(1, 5);
      System.out.println(value1);
      int value2 = string.codePointCount(7, 15);
      System.out.println(value2);
   }
}

Output:-

4
8

Java String offsetByCodePoints() Method Example

Java String class offsetByCodePoints() method returns the index within this String that is offset from the given index by codePointOffset code points.

public class Main {
   public static void main(String[] args) {
      String string = "Actions speak louder than words";
      int value1 = string.offsetByCodePoints(1, 5);
      System.out.println(value1);
      int value2 = string.offsetByCodePoints(7, 15);
      System.out.println(value2);
   }
}

Output:-

6
22

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *