當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Character codePointAt()用法及代碼示例


此方法有 3 種語法。

Java Character codePointAt(char[] a, int index) 方法

Character 類的 codePointAt() 方法用於返回 char 數組給定索引處的代碼點。如果在特定索引處,char 數組中的 char 值在高代理範圍內,則以下索引小於 char 數組的索引,如果給定索引處的 char 值在低代理範圍內,則對應的代碼點返回到這對。

用法

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

參數

上述方法需要兩個參數:

  • 字符數組
  • 數組中 char 值的索引。

返回值

codePointAt() 方法返回索引處的字符值。

例子1

public class CharactercodePointAtMethodExample1 {
	public static void main(String[] args) {
        String input = "This is the first program for the given method";
System.out.println("This is the first program for the given method: ");
int index = 4;
System.out.println(input.codePointAt(index));
    }
}

輸出:

This is the first program for the given method:
32

例子2

public class CharactercodePointAtMethodExample2 {
	public static void main(String[] args) {
		System.out.println();
		    String str = "javatpoint.com";
		System.out.println("Given String:" + str);

		// Codepoint at index 3.
		int obj1 = str.codePointAt(3);

		// Codepoint at index 7.
		int obj2 = str.codePointAt(7);

		// Prints the character at index1 in string
		System.out.println("Character Value = " + obj1);
		// Prints the character at index9 in string
		System.out.println("Character Value = " + obj2);
		
		  }
		}

輸出:

Given String:javatpoint.com
Character Value = 97
Character Value = 105

例子3

public class CharactercodePointAtMethodExample3
{
public static void main(String[] args)
{
String str1 = "JAVA";
System.out.println("The first string is = " + str1);
// Codepoint at index 1
int result1  = str1.codePointAt(1);	System.out.println("Character(unicode point) = " + result1);
String str2 = "TPOINT";
System.out.println("The second string is = " + str2);

//Codepoint at index 2.
int result2  = str1.codePointAt(2);
System.out.println("Character(unicode point) = " + result2);
}
}

輸出:

The first string is = JAVA
Character(unicode point) = 65
The second string is = TPOINT
Character(unicode point) = 86

示例 4

import java.util.Scanner;

public class CharactercodePointAtMethodExample4 {
public static void main(String[] args) {
	// initialize a new CharSequence object
	String Sequencecs = "Java is a unique language.";
	// Ask the user to enter the integer.
	System.out.println("Java is a unique language.");
	System.out.print("Enter the desired index:");
	Scanner s1 = new Scanner(System.in);
	int index = s1.nextInt();
	CharSequence cs = null;
	int result = Character.codePointAt(cs, index);
	System.out.println("Result: "+result);
	       }
}

輸出:

Java is a unique language.
Enter the desired index:8
Result: 97

Java Character codePointAt(char[] a, int index, int limit) 方法

字符類的codePointAt(char[] a, int index, int limit)方法返回char數組給定索引處的代碼點,其中隻能使用索引小於limit的元素。如果在特定索引處,char 數組中的 char 值在高代理範圍內,則以下索引小於限製。如果給定索引處的 char 值在低代理範圍內,則返回與該對對應的代碼點。

用法

public static intcodePointAt(char[] a, intindex,int limit)

參數

上述方法需要三個參數:

  • 字符數組
  • 數組中 char 值的索引。
  • 限製是可以在數組中使用的最後一個元素之後的索引。

返回值

此方法返回給定索引處的 Unicode 點。

例 5

public class CharactercodePointAtMethodExample5 {
	public static void main(String[] args) {
	char[] c1 = new char[] { 'h', 'e', 'l', 'l', 'o' };
	System.out.println("Hello");
	int index1  = 2, limit1 = 4;
	int res1 = Character.codePointAt(c1, index1, limit1);
	System.out.println("Unicode code point is:" + res1 );
	char[] c2 = new char[] { 'e', 'v', 'e', 'r', 'y','o', 'n', 'e', };
	System.out.println("Everyone");
	int index2  = 2, limit2 = 3;
	int res2 = Character.codePointAt(c1, index1, limit1);
	System.out.println("Unicode code point is:" + res2 );
	   }
	}

輸出:

Hello
Unicode code point is:108
Everyone
Unicode code point is:108

例 6

package tests;
public class CharactercodePointAtMethodExample6 {
public static void main(String[] args) {
	char[] ch = new char[] { '1', '2', '3', '4', '5' };
int index  = 1, limit = 4;
		int res = Character.codePointAt(ch, index, limit);
		System.out.println("The numbers are: '1', '2', '3', '4', '5' ");
	System.out.println("Unicode code point is " + res );
	 }
}

輸出:

The numbers are: '1', '2', '3', '4', '5' 
Unicode code point is 50

例 7

package tests;
public class CharactercodePointAtMethodExample7 {
public static void main(String[] args) {
	System.out.println("Hello");
	char[] ch1 = new char[] { 'H', 'e', 'l', 'l', 'o' };
	int index1  = 2, limit1 = 3;
	int res1 = Character.codePointAt(ch1, index1, limit1);
	System.out.println("Unicode code point for Hello is " + res1 );
		
	System.out.println("The numbers are: '1', '2', '3', '4', '5' ");
	char[] ch2 = new char[] { '1', '2', '3', '4', '5' };
	int index2  = 3, limit2 = 4;
	int res2 = Character.codePointAt(ch2, index2, limit2);
	System.out.println("Unicode code point for number is " + res1 );
		   }
}

輸出:

Hello
Unicode code point for Hello is 108
The numbers are: '1', '2', '3', '4', '5' 
Unicode code point for number is 108

Java 字符 codePointAt(CharSequence seq, int index) 方法

Character 類的 codePointAt(CharSequence seq, int index) 方法返回 charSequence 特定索引處的代碼點。如果在特定索引處,charSequence 中的 char 值在高代理範圍內,則以下索引將小於 charSequence 的長度。另一方麵,如果 charSequence 中的 char 值在低代理範圍內,則返回與給定代理對對應的補充代碼點。

用法

public static int codePointAt(CharSequence seq, int index)

參數

上述方法需要兩個參數:

  • 一係列字符值。
  • 要在 seq 中轉換的 char 值的索引。

返回值

此方法返回特定索引處的 Unicode 代碼點。

例 8

public class CharactercodePointAtMethodExample8 {
  public static void main(String[] args) {
	  System.out.println("Java is secured");  
      CharSequence s= "Java";
      int index  = 3;
      int result = Character.codePointAt(s, index);
     System.out.println("The Unicode code point is " + result);
     
     }
}

輸出:

Java is secured
The Unicode code point is 97

例 9

public class CharactercodePointAtMethodExample9 {
	public static void main(String[] args) {
		System.out.println("Java is secured");
		//System.out.println("The Unicode code point for Java is " + res1);

	      CharSequence s= "Java";
	      int index1  = 2;
	      int res1 = Character.codePointAt(s,index1);
	    System.out.println("The Unicode code point for Java is " + res1);
	     
	      System.out.println("The numbers are: '1', '2', '3', '4', '5' ");
	     // System.out.println( "The Unicode code point for number is " + res2 );
	      char[] ch = new char[] { '1', '2', '3', '4', '5' };
	       int index2 = 3;
	      int res2 = Character.codePointAt(ch, index2);
	      System.out.println( "The Unicode code point for number is " + res2 );	   
	     }
	}

輸出:

Java is secured
The Unicode code point for Java is 118
The numbers are: '1', '2', '3', '4', '5' 
The Unicode code point for number is 52

例 10

public class CharactercodePointAtMethodExample10 {
	public static void main(String[] args) {
		System.out.println("Java is more portable");	
	      CharSequence s1= "Java";
	      int index1  = 2;
	      int res1 = Character.codePointAt(s1,index1);
	    System.out.println("The Unicode code point for Java is " + res1);
	     
	    System.out.println("Java is based on the concepts of OOPS.");
		
	      CharSequence s2= "Java";
	      int index2  = 1;
	      int res2 = Character.codePointAt(s2,index2);
	    System.out.println("The Unicode code point for Java is " + res1);	      
             }	
       }

輸出:

Java is more portable
The Unicode code point for Java is 118
Java is based on the concepts of OOPS.
The Unicode code point for Java is 118



相關用法


注:本文由純淨天空篩選整理自 Java Character codePointAt() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。