Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs
    • SQL FAQ’s

Java codePointCount Method

by suresh

The Java codePointCount method is one of the Java String Methods, which is to count the number of Unicode points within the specified text range.

In this article, we will show how to use codePointCount in Java Programming language with example. The basic syntax of the codePointCount in Java Programming language is as shown below.

public int codePointCount(int Starting_Index, int End_Index) 

//In order to use in program
String_Object.codePointCount(int Starting_Index, int End_Index)
  • String_Object: Please specify the valid String Object.
  • Starting_Index: Please specify the index position of the first character.
  • End_Index: Please specify the index position of a last but one character.

Return Value

The Java codePointCount Function will count no of Unicode points from Starting_Index to End_Index – 1. Mathematically we say the return value as (End_Index – Starting_Index).

For example, codePointCount(12, 20) will return 8 as an output because 20 – 12 = 8. If we provide the Index position out of range or negative value, then the codePointCount Function will throw an error.

Java codePointCount Example

In this Java program, We are going to use the Java string codePointCount method to count the Unicode points between the specified range.

package StringFunctions;

public class CodePointCount {
	public static void main(String[] args) {
		String str = "Learn Free Java Tutorial";
		
		int a = str.codePointCount(0, 3);
		int b = str.codePointCount(4, 11);
		int c = str.codePointCount(12, str.length()- 1);
		int d = str.codePointCount(0, str.length()- 1);
		
		System.out.println( "Total Unicode Points from 0 to Index position 3 = " + a);
		System.out.println( "Total Unicode Points from 4 to Index position 11 = " + b);
		System.out.println( "Total Unicode Points from 12 to Last Index position = " + c);
		System.out.println( "Total Unicode Points from 0 to Last Index position = " + d);
	}
}

OUTPUT

Java codePointCount Method 1

ANALYSIS

Within the following codePointCount statements, the first statement will find the Count the Unicode points between 0 and 3, and the other will find Unicode points between 4 and 11.

int a = str.codePointCount(0, 3);
int b = str.codePointCount(4, 11);

If you observe the above screenshot, str.codePointCount(0, 3) is returning 3. It is because there are three Unicode points between 0 and 3 or (3 – 0 = 3).

NOTE: You should count the space as One Character.

Within the next two lines, we are using the Java codePointCount method along with the string length function to calculate the string length.

int c = str.codePointCount(12, str.length()- 1);
int d = str.codePointCount(0, str.length()- 1);

From the above String Method 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 Java System.out.println statements will print the output

System.out.println( "Total Unicode Points from 0 to Index position 3 = " + a);
System.out.println( "Total Unicode Points from 4 to Index position 11 = " + b);
System.out.println( "Total Unicode Points from 12 to Last Index position = " + c);
System.out.println( "Total Unicode Points from 0 to Last Index position = " + d);

Placed Under: Java

  • SQL DML, DDL, DCL & TCL Cmds
  • SQL NOT EXISTS Operator
  • SQL UPDATE from SELECT
  • SQL AFTER UPDATE Triggers
  • SQL Get Column Names in Table
  • SQL IF ELSE
  • SQL ACID Properties
  • SQL FOR XML PATH
  • Java Two Dimensional Array
  • Java Perfect Number Program
  • Java Count Digits in a Number
  • C Compare Two Strings Program
  • C Print Prime Numbers 1 to 100
  • C program to Reverse a String
  • C Palindrome Number Program
  • C Program for Palindrome String
  • C Remove Duplicate String Chars
  • C Square of a Number Program
  • C Sum and Average of N Number
  • Python Fibonacci Series program
  • Python Area Of Circle Program
  • Python Prime Numbers 1 to 100
  • Python Program for Leap Year
  • Tableau Rank Calculation
  • Tableau Google Maps usage
  • Power BI Format Dates
  • Power BI Top 10 Filters
  • Power BI – Create Hierarchy
  • Power BI DAX Math Functions
  • Learn SSIS in 28 Days
  • SSIS Transformations
  • SSIS Incremental Load
  • SSRS Drill Through Reports
  • SSRS Drill Down Reports
  • R Programming Tutorial
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy