Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

offsetByCodePoints()

Example

Retrieve an index within a string that is shifted from another index by a specified number of code points.

String myStr = "Hello, World!";
int result = myStr.offsetByCodePoints(3, 2);
System.out.println(result);

Definition and Usage

The offsetByCodePoints() method provides an index within a string that is shifted from another index by a specified number of code points.

Note: A code point might consist of multiple characters, resulting in an offset of more than 1 for the index.

Syntax

public int offsetByCodePoints(int index, int codePointOffset)

Parameter Values

Parameter

Description

Index

Necessary. Specifies the index in the string from which the offset is measured.

Codepoint offset

Mandatory. Indicates the number of code points by which to offset. Positive values will result in a number greater than the index, while negative values will yield a number less than the index.

Technical Details

Returns:

An integer representing the index that is offset by a number of code points from the given index.

Throws:

An IndexOutOfBoundsException will be thrown if the index is negative or exceeds the length of the string, or if there are insufficient code points in the string to offset by the specified codePointOffset.

Java Version

1.5