w3resource logo
Java Programming Exercies

Java String Exercises: Count a number of Unicode code points in the specified text range of a String

Java String : Exercise-4 with Solution

Write a java program to count a number of Unicode code points in the specified text range of a String.

Sample Solution:

Java Code:

public class Exercise4 {

 public static void main(String[] args) {
  
    String str = "w3rsource.com";
    System.out.println("Original String : " + str);
        
    // codepoint from index 1 to index 10
    int ctr = str.codePointCount(1, 10);
        
    // prints character from index 1 to index 10
    System.out.println("Codepoint count = " + ctr);
  }
}

Sample Output:

Original String : w3rsource.com                                                                               
Codepoint count = 9

Java Practice online:

public class Exercise4 {

 public static void main(String[] args) {
  
    String str = "w3rsource.com";
    System.out.println("Original String : " + str);
        
    // codepoint from index 1 to index 10
    int ctr = str.codePointCount(1, 10);
        
    // prints character from index 1 to index 10
    System.out.println("Codepoint count = " + ctr);
  }
}

Improve this sample solution and post your code through Disqus