Java Character.charPointBefore()方法

原文:https://www.studytonight.com/java-wrapper-class/java-codepointbefore-char-int-method

Java charPointBefore()方法是Character类的一部分。该方法返回字符数组指定索引前的码点。必须注意的是,如果字符数组中位于(索引-1) 的字符值在低代理范围内,(索引-2) 不是负数,并且如果字符数组中位于(索引-2) 的字符值在高代理范围内,则返回指向该代理对的补充代码点。否则,返回(索引-1) 处的字符值。

语法:

public static int codePointBefore(char[] a, int index)

参数:

传递的参数是char数组和int索引,在此之前将返回字符数组中字符值的代码点。

返回:

返回指定索引前字符值中char值的码点。

例 1:

这里,返回指定索引之前的字符的代码点值。

import java.lang.Character;

public class StudyTonight
{  
    public static void main(String[] args) 
    {  
        char[] ch = new char[] { 'm', 'o', 'h', 'i', 't' };
        int index = 4;  

        int r = Character.codePointBefore(ch,index);

        System.out.println("The codepoint value is : "+r);
    }  
}

码点值为:105

例 2:

这里有一个用户定义的例子,任何使用这段代码的人都可以输入自己选择的值,并获得想要的输出。

import java.lang.Character;
import java.util.Scanner;

public class StudyTonight
{  
    public static void main(String[] args) 
    {  
        try
        {
          System.out.println("Enter the characters");
          Scanner sc = new Scanner(System.in);
          char[] ch = sc.next().toCharArray();
          System.out.println("Enter index");
          int n = sc.nextInt();
          System.out.println("The Codepoint value is : "+Character.codePointBefore(ch,n));  
        }
        catch(Exception e)
        {
          System.out.println("Invalid Input");
        }
    }  
}

输入字符 今晚学习 输入索引 3 码点值为:117 *输入字符 大神 输入索引 6 码点值为:115

                                  • *输入字符 0x667 输入索引 7 无效输入

实时示例:

在这里,您可以测试实时代码示例。您可以为不同的值执行示例,甚至可以编辑和编写您的示例来测试 Java 代码。