- Java.lang 包类
- Java.lang - 首页
- 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 包扩展
- Java.lang - 接口
- Java.lang - 错误
- Java.lang - 异常
- Java.lang 包有用资源
- Java.lang - 有用资源
- Java.lang - 讨论
Java - Character offsetByCodePoints() 方法
描述
Java 的 Character offsetByCodePoints() 方法用于在给定的 char 子数组中检索相对于给定索引由代码点偏移量偏移的索引。
子数组在 char 数组中指定,通过从某个起始索引开始计算字符。
文本范围内的未配对代理应被视为每个代码点。
此方法以两种多态形式出现,具有相同的返回类型但参数不同。
语法
以下是 Java Character offsetByCodePoints() 方法的语法
public static int offsetByCodePoints(char[] a, int start, int count, int index, int codePointOffset) (or) public static int offsetByCodePoints(CharSequence seq, int index, int codePointOffset)
参数
a − char 数组
start − 子数组第一个 char 的索引
count − 子数组的 char 长度
index − 要偏移的索引
seq − 字符序列
codePointOffset − 代码点偏移量
返回值
此方法返回子数组中的索引
获取代码点偏移量示例
以下示例演示了 Java Character offsetByCodePoints(char[] a, int start, int count, int index, int codePointOffset) 方法的使用。我们创建了一个 char 数组并用 char 值初始化。使用 offsetByCodePoints() 方法,我们检索了偏移量并打印了结果。
package com.tutorialspoint; public class CharacterDemo { public static void main(String[] args) { // create a char array c and assign values char[] c = new char[] { 'a', 'b', 'c', 'd', 'e', 'f' }; // craete 2 int primitives start, count and assign values int start = 1; int count = 5; // create an int primitive res int res; // assign result of offsetByCodePoints on subarray of c to res res = Character.offsetByCodePoints(c, start, count, 2, 4); String str = "The index within the subarray of c is " + res; // print res value System.out.println( str ); } }
输出
让我们编译并运行以上程序,这将产生以下结果:
The index within the subarray of c is 6
获取代码点偏移量示例
以下示例演示了 Java Character offsetByCodePoints(CharSequence seq, int index, int codePointOffset) 的使用。我们创建了一个 CharSequence 变量并用一个字符串初始化。使用 offsetByCodePoints() 方法,我们检索了偏移量并打印了结果。
package com.tutorialspoint; public class CharacterDemo { public static void main(String[] args) { // create a CharSequence seq and assign value CharSequence seq = "Hello World"; // create an int primitive res int res; // assign result of offsetByCodePoints on seq to res res = Character.offsetByCodePoints(seq, 3, 8); String str = "The index within the char sequence seq is " + res; // print res value System.out.println( str ); } }
输出
让我们编译并运行以上程序,这将产生以下结果:
The index within the char sequence seq is 11
从数字 char 数组获取代码点偏移量示例
以下示例演示了 Java Character offsetByCodePoints(char[] a, int start, int count, int index, int codePointOffset) 方法的使用。我们创建了一个 char 数组并用 char 值初始化。使用 offsetByCodePoints() 方法,我们检索了偏移量并打印了结果。
package com.tutorialspoint; public class CharacterDemo { public static void main(String[] args) { char[] c = new char[] { '0', '1', '2', '3', '4', '5' }; int start = 1; int count = 3; int result = Character.offsetByCodePoints(c, start, count, 1, 2); System.out.println("The index within the subarray of c is " + result); } }
输出
编译并执行上述程序后的输出如下所示:
The index within the subarray of c is 3
在从 char 数组获取代码点偏移量时遇到 IndexOutOfBoundsException 示例
在某些情况下,此方法也会抛出异常。在此示例中,我们将看到方法抛出 IndexOutOfBoundsException 的场景。
public class CharacterDemo { public static void main(String[] args) { char[] c = new char[] { '0', '5', '2', 'a', 'z', '/' }; int start = 0; int count = 3; int result = Character.offsetByCodePoints(c, start, count, 2, 4); System.out.println("The index within the subarray of c is " + result); } }
异常
编译并运行以上程序后,获得的输出如下:
Exception in thread "main" java.lang.IndexOutOfBoundsException at java.lang.Character.offsetByCodePointsImpl(Character.java:5388) at java.lang.Character.offsetByCodePoints(Character.java:5372) at CharacterDemo.main(CharacterDemo.java:10)
在从 char 数组获取代码点偏移量时遇到 NullPointerException 示例
现在,让我们看看方法抛出 NullPointerException 的另一种场景。
public class CharacterDemo { public static void main(String[] args) { char[] c = new char[2]; c = null; int start = 0; int count = 0; int result = Character.offsetByCodePoints(c, start, count, 0, 0); System.out.println("The index within the subarray of c is " + result); } }
异常
编译并运行以上程序,输出将显示为:
Exception in thread "main" java.lang.NullPointerExceptionat java.lang.Character.offsetByCodePoints(Character.java:5368)at CharacterDemo.main(CharacterDemo.java:11)