Java 的 offsetByCodePoints 方法是 String 方法之一,用于返回字符串中相对于指定索引偏移 codePointOffset 个代码点的索引。String offsetByCodePoints 的基本语法如下所示。
public int offsetByCodePoints(int index, int codePointOffset) // It will return the integer Value as Output //To use in program String_Object.offsetByCodePoints(int index, int codePointOffset)
- index:请指定要偏移的索引。
- codePointOffset:请指定代码点偏移量。
Java offsetByCodePoints 方法示例
String.offsetByCodepoints 方法返回字符串中相对于指定索引偏移 codePointOffset 个代码点的索引。在此 Java 程序中,我们将找到相同的内容。
package StringFunctions;
public class offsetByCodePointsMethod {
public static void main(String[] args) {
String str = "Hello";
int a = str.offsetByCodePoints(1, 2);
int b = "Tutorial GateWay".offsetByCodePoints(1, 12);
int c = "Free Java Tutorial at Tutorial GateWay".offsetByCodePoints(10, 22);
int d = "Java Programming Language".offsetByCodePoints(5, 12);
System.out.println("The New Index of a String Str = " + a);
System.out.println("The New Index = " + b);
System.out.println("The New Index = " + c);
System.out.println("The New Index = " + d);
}
}

在此 Java 程序中,首先,我们声明 String 变量并使用以下语句为其赋值。
String str = "Hello";
以下语句调用 public int offsetByCodePoints(int index, int codePointOffset) 方法来查找上述字符串中的索引。
int a = str.offsetByCodePoints(1, 2);
接下来,我们直接在字符串上应用 offsetByCodepoints String 方法,然后将这些字符串的新索引值赋给整数变量 b、c 和 d。
int b = "Tutorial GateWay".offsetByCodePoints(1, 12); int c = "Free Java Tutorial at Tutorial GateWay".offsetByCodePoints(10, 22); int d = "Java Programming Language".offsetByCodePoints(5, 12);
以下 Java System.out.println 语句将打印输出。