当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。