Java Character.codePointAt()
方法
原文:https://www.studytonight.com/java-wrapper-class/java-character-codepointbefore-char-int-int-method
Java codePointAt()
方法是Character
类的一部分。此方法返回指定索引之前的字符数组中字符值的代码点,其中只能使用索引大于或等于 start 的数组元素。必须注意的是,如果在(索引-1 )处的字符数组中的字符值在低代理范围内,(索引-2 )不小于 start,并且如果在(索引-2 )处的字符数组中的字符值在高代理范围内,则返回对应于该代理对的补充代码点。否则,返回(索引-1 )处的字符值。
语法:
public static int codePointBefore(char[] a, int index, int start)
参数:
传递的参数是:
char
数组int
返回前一个代码点值所依据的索引int
start,它是 char 数组中第一个数组元素的索引。
返回:
返回字符数组中第一个数组元素的索引。
例 1:
这里,返回指定索引之前的字符的代码点值。
public class StudyTonight
{
public static void main(String args[])
{
char[] ch1 = new char[] { 'm', 'o', 'h', 'i', 't' };
System.out.println("The input is : Mohit");
int i1 = 2, s1 = 1;
int r1 = Character.codePointBefore(ch1, i1, s1);
System.out.println("Unicode code point is : " + r1 );
char[] ch2 = new char[] { 's', 't', 'u', 'd', 'y','t', 'o', 'n','i','g','h','t' };
System.out.println("The input is : StudyTonight");
int i2 = 2, s2 = 1;
int r2 = Character.codePointBefore(ch2, i2, s2);
System.out.println("Unicode code point is : " + r2 );
}
}
输入为:莫希特 Unicode 码点为:111 输入为:今晚学习 Unicode 码点为:116
例 2:
这里有一个用户定义的例子,任何使用这段代码的人都可以输入自己选择的值,并获得想要的输出。
import java.lang.Character;
import java.util.Scanner;
public class StudyTonight
{
public static void main(String[] args)
{
try
{
System.out.println("Enter char array");
Scanner sc = new Scanner(System.in);
char []ch = sc.nextLine().toCharArray();
System.out.println("Enter index");
int n = sc.nextInt();
System.out.println("Enter start");
int s = sc.nextInt();
System.out.println("The codepoint value is : " +Character.codePointBefore(ch,n,s));
}
catch(Exception e)
{
System.out.println("Invalid Input");
}
}
}
进入 char 数组 corporation 进入 index 3 进入 start 2 码点值为:114
- T8】进入 char 数组 delpc 进入 index 2 进入 start 1 码点值为:101
实时示例:
在这里,您可以测试实时代码示例。您可以为不同的值执行示例,甚至可以编辑和编写您的示例来测试 Java 代码。