
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java StringBuilder offsetByCodePoints() Method
The Java StringBuilder offsetByCodePoints() method is, used to retrieve the index value within this sequence that is offset from the given index by codePointOffset code points.
This method returns a stream of code point values of the sequence character. A stream contains an integer code point values, which is similar to the ASCII value in Java.
The offsetByCodePoints() method accepts two parameters as an integer that holds the values of index and codePointOffset. It throws different exceptions if the index value is negative or greater than the sequence length, and so on.
Syntax
Following is the syntax of the Java StringBuilder offsetByCodePoints() method −
public int offsetByCodePoints(int index, int codePointOffset)
Parameters
index − This is the index to be offset.
codePointOffset − This is the offset in code points.
Return Value
This method returns the index within this sequence.
Example: Getting Offset By CodePoint
If the given index value is positive and less than the sequence length, the offsetByCodePoint() method returns the index value.
In the following program, we are creating an object of the StringBuilder class with the value of TutorialsPoint. Using the offsetByCodePoints(), we are trying to get the index value at the specified startIndex 2, and the codePointOffset 5.
public class OffsetCodePoint { public static void main(String[] args) { //create an object of the StringBuilder class StringBuilder sb = new StringBuilder("TutorialsPoint"); System.out.println("The given String is: " + sb); //initialize the index and codePointOffset values int index = 2; int codePointOffset = 5; System.out.println("The given index and code point offset values are: " + index + " and " + codePointOffset); //using the offsetByCodePoint() method System.out.println("The index is: " + sb.offsetByCodePoints(index, codePointOffset)); } }
Output
On executing the above program, it will produce the following result −
The given String is: TutorialsPoint The given index and code point offset values are: 2 and 5 The index is: 7
Example: Facing IndexOutOfBoundsException while Getting Offset By CodePoint
If the given index value is negative and greater than the sequence length, the offsetByCodePoint() method throws an IndexOutOfBoundsException.
If the following program, we are instantiating the StringBuilder class with the value Java Programming. Using the offsetByCodePoints() method, we are trying to get the index value at the specified index -1 and codePointOffset 5.
public class OffsetCodePoint { public static void main(String[] args) { try { //instantiate the StringBuilder class StringBuilder sb = new StringBuilder("Java Programming"); System.out.println("The given String is: " + sb); //initialize the index and codePointOffset values int index = -1; int codePointOffset = 5; System.out.println("The given index and code point offset values are: " + index + " and " + codePointOffset); //using the offsetByCodePoint() method System.out.println("The index is: " + sb.offsetByCodePoints(index, codePointOffset)); } catch(IndexOutOfBoundsException e) { e.printStackTrace(); System.out.println("Exception e: " + e); } } }
Output
Following is the output of the above program −
The given String is: Java Programming The given index and code point offset values are: -1 and 5 java.lang.IndexOutOfBoundsException at java.base/java.lang.AbstractStringBuilder.offsetByCodePoints(AbstractStringBuilder.java:461) at java.base/java.lang.StringBuilder.offsetByCodePoints(StringBuilder.java:279) at OffsetCodePoint.main(OffsetCodePoint.java:15) Exception e: java.lang.IndexOutOfBoundsException
Example: Facing IndexOutOfBoundsException while Getting Offset By CodePoint
If the given index value is larger than the sequence length, this method throws the IndexOutOfBoundException.
In the following example, we are creating a StringBuilder with the value Hello. Using the offsetByCodePoints() method, we are, trying to get the index value at the specified startIndex 10, and codePointOffset 2.
public class OffsetCodePoint { public static void main(String[] args) { try { //instantiate the StringBuilder class StringBuilder sb = new StringBuilder("Hello"); System.out.println("The given String is: " + sb); //initialize the index and codePointOffset values int index = 10; int codePointOffset = 2; System.out.println("The given index and code point offset values are: " + index + " and " + codePointOffset); //using the offsetByCodePoint() method System.out.println("The index is: " + sb.offsetByCodePoints(index, codePointOffset)); } catch(IndexOutOfBoundsException e) { e.printStackTrace(); System.out.println("Exception e: " + e); } } }
Output
The above program produces the following output −
The given String is: Hello The given index and code point offset values are: 10 and 2 java.lang.IndexOutOfBoundsException at java.base/java.lang.AbstractStringBuilder.offsetByCodePoints(AbstractStringBuilder.java:461) at java.base/java.lang.StringBuilder.offsetByCodePoints(StringBuilder.java:279) at OffsetCodePoint.main(OffsetCodePoint.java:15) Exception e: java.lang.IndexOutOfBoundsException