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 StringBuffer codePointCount() Method
Selected Reading
Java StringBuffer codePointCount() MethodThe Java StringBuffer codePointCount() method counts the number of Unicode code points in the specified text range of this sequence. The text range begins at the specified starting index and extends to the char at second to the last index. Thus the length (in chars) of the text range is ending index beginning index. If there is no Unicode code point present in the given range of text, the method will not throw any errors and just prints 0. Note − Unpaired surrogate code points are considered one separate code point each. SyntaxFollowing is the syntax for Java StringBuffer 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. Example: Getting length of Text RangeWhen we consider the input text as the alphabet, the method returns the length of the text range given as arguments. The following example shows the usage of Java StringBuffer codePointCount() method. package com.tutorialspoint;
public class StringBufferDemo {
public static void main(String[] args) {
StringBuffer buff = new StringBuffer("TUTORIALS");
System.out.println("buffer = " + buff);
// returns the codepoint count from index 1 to 5
int retval = buff.codePointCount(1, 5);
System.out.println("Count = " + retval);
buff = new StringBuffer("7489042 ");
System.out.println("buffer = " + buff);
// returns the codepoint count from index 3 to 9
retval = buff.codePointCount(3, 9);
System.out.println("Count = " + retval);
buff = new StringBuffer("@#$%^&");
System.out.println("buffer = " + buff);
// returns the codepoint count from index 2 to 4
retval = buff.codePointCount(2, 4);
System.out.println("Count = " + retval);
}
}
OutputLet us compile and run the above program, this will produce the following result − buffer = TUTORIALS Count = 4 buffer = 7489042 Count = 6 buffer = @#$%^& Count = 2 Example: Getting length of Text Range having no Valid CodePointsWhen we consider the input text as characters that do not have valid codepoints, the method returns zero. public class StringBufferDemo {
public static void main(String[] args) {
StringBuffer buff = new StringBuffer("/u1298139");
System.out.println("buffer = " + buff);
// returns the codepoint count
int retval = buff.codePointCount(0, 0);
System.out.println("Count = " + retval);
}
}
OutputLet us compile and run the above program, this will produce the following result − buffer = /u1298139 Count = 0 Example: Facing Exception while checking count of CodePointsHowever, if the index arguments given exceed or precede the text range, the method throws an IndexOutOfBounds Exception. public class StringBufferDemo {
public static void main(String[] args) {
StringBuffer buff = new StringBuffer("djk137");
System.out.println("buffer = " + buff);
// returns the codepoint count from index 2 to 4
int retval = buff.codePointCount(-1, 9);
System.out.println("Count = " + retval);
}
}
ExceptionIf we compile and run the program, an IndexOutOfBounds Exception is thrown instead of printing the output − buffer = djk137 Exception in thread "main" java.lang.IndexOutOfBoundsException at java.lang.AbstractStringBuilder.codePointCount(AbstractStringBuilder.java:320) at java.lang.StringBuffer.codePointCount(StringBuffer.java:227)at StringBufferDemo.main(StringBufferDemo.java:11) java_lang_stringbuffer.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. |