在给定索引处确定 Unicode 编码点的 Java 程序
在本文中,我们将了解如何确定给定索引处的 unicode 编码点。每个字符都由 unicode 编码点表示。编码点是一个整数,可唯一标识给定字符。Unicode 字符可以使用不同的编码(如 UTF-8 或 UTF-16)进行编码。
以下是相同的演示 -
假设我们的输入为 -
Input String: Java Program Index value: 5
所需的输出为 -
Unicode Point: 80
算法
Step 1 - START Step 2 - Declare a string value namely input_string and two integer values namely index and result Step 3 - Define the values. Step 4 - Use the function codePointAt() to fetch the code point value. Store the value as result. Step 5 - Display the result Step 6 - Stop
示例 1
在这里,我们将所有操作绑定在“主”函数下。
import java.io.*; public class UniCode { public static void main(String[] args){ System.out.println("Required packages have been imported"); String input_string = "Java Program"; System.out.println("\nThe string is defined as: " +input_string); int result = input_string.codePointAt(5); System.out.println("The unicode point at index 5 is : " + result); } }
输出
Required packages have been imported The string is defined as: Java Program The unicode point at index 5 is : 80
示例 2
在这里,我们将操作封装到函数中,以展示面向对象编程。
import java.io.*; public class UniCode { static void unicode_value(String input_string, int index){ int result = input_string.codePointAt(index); System.out.println("The unicode point at index " +index +"is : " + result); } public static void main(String[] args) { System.out.println("Required packages have been imported"); String input_string = "Java Program"; System.out.println("\nThe string is defined as: " +input_string); int index = 5; unicode_value(input_string, index); } }
输出
Required packages have been imported The string is defined as: Java Program The unicode point at index 5is : 80
广告