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 StringBuilder codePointAt() Method
Selected Reading
Java StringBuilder codePointAt() MethodThe Java StringBuilder codePointAt() method is used to get the character (its Unicode code point) present at an index of a StringBuilder. The index ranges from 0 to length() - 1. Note − If the char value specified at the given index is in the high-surrogate range and its following index is less than the length of this sequence, and the char value at this following index is in the low-surrogate range, then the supplementary code point corresponding to this surrogate pair is returned. Otherwise, the char value at the given index is returned. SyntaxFollowing is the syntax for Java StringBuilder codePointAt() method public int codePointAt(int index) Parameters
Return ValueThis method returns the code point value of the character at the index. Example: Getting codePoint at given IndexIf we initialize the StringBuilder object with the letters as CharSequence, the method returns the character present in the argument index of the StringBuilder. The following example shows the usage of Java StringBuilder codePointAt() method. package com.tutorialspoint;
public class StringBuilderDemo {
public static void main(String[] args) {
StringBuilder buff = new StringBuilder("TUTORIALS");
System.out.println("buffer = " + buff);
// returns the codepoint at index 5
int retval = buff.codePointAt(5);
System.out.println("Character(unicode point) = " + retval);
buff = new StringBuilder("amrood admin ");
System.out.println("buffer = " + buff);
// returns the codepoint at index 6 i.e whitespace character
retval = buff.codePointAt(6);
System.out.println("Character(unicode point) = " + retval);
}
}
OutputLet us compile and run the above program, this will produce the following result − buffer = TUTORIALS Character(unicode point) = 73 buffer = amrood admin Character(unicode point) = 32 Example: Getting codePoint at given IndexIn another example, we are creating a StringBuilder object and initializing it with a digit sequence. Then, the method is invoked on this object with an index as its argument. public class StringBuilderCodePointAt {
public static void main(String[] args) {
StringBuilder obj = new StringBuilder("78343890");
int id = 5;
System.out.println("The code point at index " + id + " is: " + obj.codePointAt(id));
}
}
OutputOnce we compile and run the program above, the output is displayed as follows − The code point at index 5 is: 56 Example: Getting codePoint at given IndexIf we initialize the StringBuilder object with the symbols as CharSequence, and an index is passed as the argument, the method returns the character present at that index. public class StringBuilderCodePointAt {
public static void main(String[] args) {
StringBuilder obj = new StringBuilder("#$%^&*(");
int id = 3;
System.out.println("The code point at index " + id + " is: " + obj.codePointAt(id));
}
}
OutputThe output for the above program is printed as − The code point at index 3 is: 94 Example: Facing Exception while Getting codePoint at Negative IndexIf the index passed as an argument to this method is invalid (either negative or greater than charsequence length), an exception is thrown. public class StringBuilderCodePointAt {
public static void main(String[] args) {
StringBuilder obj = new StringBuilder("Tutorialspoint");
int id = -2;
System.out.println("The code point at index " + id + " is: " + obj.codePointAt(id)); //throws exception
}
}
ExceptionIf we try to compile and run the program above, an exception is thrown instead of printing the output − Exception in thread "main" java.lang.StringIndexOutOfBoundsException: Index -2 out of bounds for length 14
at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302)
at java.base/java.lang.String.checkIndex(String.java:4570)
at java.base/java.lang.AbstractStringBuilder.codePointAt(AbstractStringBuilder.java:389)
at java.base/java.lang.StringBuilder.codePointAt(StringBuilder.java:252)
at StringBuilderCodePointAt.main(StringBuilderCodePointAt.java:11)
java_lang_stringbuilder.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. |