GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Java - String codePointCount() Method
Selected Reading
Java - String codePointCount() MethodDescriptionThe Java String codePointCount() method is used to return the number(count) of Unicode code points in the specified text range of this String. The text range begins at the specified beginIndex and extends to the char at index endIndex - 1. Therefore, the length (in chars) of the text range is endIndex-beginIndex. The indexes are referring the character position in the string, and the first char value denotes the 0th index, and the second char value denotes the 1st index, and so on. The codePointCount() method accept two parameters as an integer that holds the beginIndex and endIndex value. SyntaxFollowing is the syntax of the Java String codePointCount() method − public int codePointCount(int beginIndex, int endIndex) Parameters
Return ValueThis method returns the number of Unicode code points in the specified text range.
Getting Code Point Count from a String for Zero Indexes ExampleIf the given beginIndex and endIndex value are zero, the codePointcount() method return zero. In the following program, we are creating an object of the string class with the value HelloWorld. Then, using the codePointCount() method, we are trying to count the character code point value at the given range (beginIndex = 0, endIndex = 0). package com.tutorialspoint;
public class Demo {
public static void main(String[] args) {
//create an object of the string class
String str = new String("HelloWorld");
System.out.println("The given string is: " + str);
//initialize the beginIndex and endIndex value
int beginIndex = 0;
int endIndex = 0;
System.out.println("The given beginIndex and endIndex values are: " + beginIndex + " and " + endIndex);
//using codePointCount() method
System.out.println("The count of character code point at the given range " + "(" + beginIndex + "," + endIndex + ") is: " + str.codePointCount(beginIndex, endIndex));
}
}
OutputOn executing the above program, it will produce the following result − The given string is: HelloWorld The given beginIndex and endIndex values are: 0 and 0 The count of character code point at the given range (0,0) is: 0 Checking Exception while getting Code Point Count from a String ExampleIf the given beginIndex value is greater than the endIndex value, this method throws IndexOutOfBoundException. In this example, we are creating a string literal with the value JavaProgramming. Then, using the codePointCount() method, we are trying to print the count of the character code point at the given range, since the beginIndex value is greater than the endIndex the method throws an exception. public class Demo {
public static void main(String[] args) {
try {
//string initialization
String str = "Welcome to Tutorials Point";
System.out.println("The string value is: " + str);
// initialize the beginIndex and endIndex value
int beginIndex = 10, endIndex = 5;
System.out.println("The beginIndex and endIndex values are: " + beginIndex + " and " + endIndex);
//using the codePointCount() method
System.out.println("The count of character code point at the given range " + "(" + beginIndex + "," + endIndex + ") is: " + str.codePointCount(beginIndex, endIndex));
} catch (Exception e) {
e.printStackTrace();
System.out.println("The exception is: " + e);
}
}
}
OutputFollowing is the output of the above program − The string value is: Welcome to Tutorials Pointjava.lang.IndexOutOfBoundsException
at java.base/java.lang.String.codePointCount(String.java:1610)
at TP.main(TP.java:16)
The exception is: java.lang.IndexOutOfBoundsException
Getting Code Point Count from a String for Non-Zero indexes ExampleIf the given beginIndex and endIndex value are positive and less than the string length, the codePointCount () method return the count of the character code point at the given range. In the following example, we are instantiating the string class with the value TutorialsPoint. Then, using the codePointCount() method, we are trying to count the character code point value at the given range ( beginIndex = 2, endIndex = 6). package com.tutorialspoint.String;
import java.lang.*;
public class Test {
public static void main (String [] args) {
// instantiate the String class
String str = new String("TutorialsPoint");
System.out.println("The given string is: " + str);
//initialize the index values
int beginIndex = 2, endIndex = 6;
System.out.println("The beginIndex and endIndex values are: " + beginIndex + " and " + endIndex);
System.out.print(
"The count of the character code point at the given range " + "(" + beginIndex + "," + endIndex + ") is: "+ str.codePointCount(beginIndex, endIndex));
}
}
OutputThe above program, produces the following results − The given string is: TutorialsPoint The beginIndex and endIndex values are: 2 and 6 The count of the character code point at the given range (2,6) is: 4 java_lang_string.htm Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |