在給定索引處確定 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

更新於: 29-Mar-2022

329 檢視

開啟您的 職業生涯

完成課程即可獲得認證

開始
廣告