Java Character.codePointAt()方法

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

Java codePointAt()方法是Character类的一部分。此方法返回字符数组中指定索引处的代码点。如果在特定索引处,给定索引处的字符值在低代理范围内,则返回与该对对应的代码点;否则,如果字符数组中的字符值在高代理范围内,则返回小于字符数组索引的以下索引。

语法:

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

参数:

传递参数有:

  • char

  • int字符数组中字符值的索引。

返回:

返回传递的索引处字符的代码点值。

例 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.codePointAt(ch,index);
        System.out.println("The codepoint value is : "+r);
    }  
}

码点值为:116

例 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.codePointAt(ch,n));       
        }
        catch(Exception e)
        {
          System.out.println("Invalid Input");
        }
    }  
}

输入字符 今晚学习 输入索引 2 码点值为:117 *输入字符 快乐 输入索引 4 码点值为:121

实时示例:

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