介绍
codePointAt(CharSequence seq, int index)
方法用于获取 CharSequence
中指定索引处字符的 Unicode 码点。
💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版
codePointAt(CharSequence seq, int index)
方法用于获取 CharSequence
中指定索引处字符的 Unicode 码点。
在 ~/project
目录下使用以下命令创建一个名为 CodePointAtDemo.java
的 Java 程序文件:
touch ~/project/CodePointAtDemo.java
在 CodePointAtDemo.java
文件中编写以下 Java 代码:
import java.lang.Character;
import java.util.Scanner;
public class CodePointAtDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scan.nextLine();
System.out.print("Enter an index: ");
int index = scan.nextInt();
char ch = input.charAt(index);
int codePoint = Character.codePointAt(input, index);
System.out.printf("The Unicode code point of '%c' at index %d is %d", ch, index, codePoint);
}
}
在上述代码中,我们导入了 Character
类和 Scanner
类。然后创建了一个 main
方法,该方法接收用户输入,获取输入字符串中索引 index
处字符的 Unicode 码点,并显示该字符及其 Unicode 码点。
通过运行以下命令编译 CodePointAtDemo.java
程序:
javac ~/project/CodePointAtDemo.java
编译成功后,使用以下命令运行程序:
java CodePointAtDemo
你将看到以下提示:
Enter a string:
输入你选择的字符串并按回车键。你将看到以下提示:
Enter an index:
输入你选择的索引并按回车键。程序将显示指定索引处的字符及其 Unicode 码点。
例如:
Enter a string: Hello world
Enter an index: 1
The Unicode code point of 'e' at index 1 is 101
恭喜!你已经成功完成了 Java Character Codepointat Charsequence Int Method 实验。你学会了如何在 Java 编程语言中使用 Character
类的 codePointAt(CharSequence seq, int index)
方法来获取 CharSequence
中指定索引处字符的 Unicode 码点。