Java Character codePointBefore() 方法

2024年11月6日 | 阅读 6 分钟

此方法有 3 种语法。

Java codePointBefore(char[] a, int index) 方法

Character 类的 codePointBefore(char[] a, int index) 方法返回 char 数组中给定索引的代码点。如果 char 数组中(index-1)处的 char 值处于低代理范围,则 index-2 不为负数。如果 char 数组中(index-2)处的 char 值处于高代理范围,则返回此代理对的补充代码点。否则,返回(index-1)处的 char 值。

语法

参数

上述方法需要两个参数

  • char 数组
  • 数组中需要返回其 char 值的索引。

返回值

codePointBefore(char[] a, int index) 方法返回给定索引之前的 Unicode 点。

示例 1

输出

Welcome to java tutorials.
The unicode point is given as = 87

示例 2

输出

Welcome to java tutorials.
Unicode code point before the string is 87
The numbers are:  '1', '2', '3', '4', '5' 
Unicode code point before the number is 108

示例 3

输出

Enter the desired index: 3
Result is given by:39

Java Character codePointBefore(char[] a, int index, int start) 方法

Character 类的 codePointBefore(char[] a, int index, int start) 方法用于返回 char 数组中给定索引之前的代码点,其中只有索引大于或等于 start 的数组元素才能被使用。如果在 char 数组中,(index-1)处的 char 值处于低代理范围,则(index-2)不小于 start。另一方面,如果(index-2)处的 char 值处于高代理范围,则返回与代理对相对应的补充代码点。否则,它返回(index-1)处的 char 值。

语法

参数

上述方法需要三个参数

  • char 数组。
  • 需要返回代码点的索引。
  • start 是 char 数组中第一个数组元素的索引。

返回值

codePointBefore(Char[] a, int index, int start) 方法返回索引之前的 Unicode 代码点。

示例 4

输出

Welcome to our tutorial site.
The unicode code point is 67

示例 5

输出

Welcome to our tutorial site.
The unicode code point for alphabets:
The unicode code point is 67
The unicode code point for numbers:
The unicode code point is 51

示例 6

输出

The numbers are: '1', '2', '3', '4'
The unicode code point is 51
The numbers are: '5', '6', '7', '8'
The unicode code point is 55

Java Character codePointBefore(charSequence seq, int index) 方法

Character 类的 codePointBefore(charSequence seq, int index) 方法用于返回 charSequence 中给定索引之前的代码点。如果在 char 序列中,(index-1)处的 char 值处于低代理范围,则(index-2)不为负数。另一方面,如果(index-2)处的 char 值处于高代理范围,则返回与此代理对相对应的补充代码点。否则,返回(index-1)处的 char 值。

语法

参数

上述方法需要两个参数

  • charSequence 实例。
  • 需要返回代码点的索引。

返回值

codePointBefore(charaSequence seq, int index) 方法返回给定索引之前的 Unicode 代码点值。

示例 7

输出

Welcome to our tutorial site:
The Unicode code point for Java is 108
The Unicode code point for Everyone is 101

示例 8

输出

The string is: Java
The Unicode code point for Java is 108
The numbers are: '1', '2', '3', '4'
The unicode code point for numbers is 108

示例 9

输出

The numbers are: '1', '2', '3', '4':
The unicode code point for first four numbers is 51
The numbers are: '1', '2', '3', '4':
The unicode code point for second four numbers is 55