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


Java Character codePointBefore()用法及代码示例


此方法有 3 种语法。

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

Character 类的 codePointBefore(char[] a, int index) 方法返回 char 数组给定索引的代码点。如果 char 数组中 (index-1) 处的 char 值在低代理范围内,则 index-2 不为负。如果 char 数组中 (index-2) 处的 char 值在高代理范围内,则返回此代理对的补充代码点。否则,返回 (index-1) 处的 char 值。

用法

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

参数

上述方法需要两个参数:

  • 字符数组
  • 数组中需要返回的字符值的索引。

返回值

codePointBefore(char[] a, int index) 方法返回给定索引之前的 Unicode 点。

例子1

public class CharactercodePointBeforeMethodExample1 {
   public static void main(String[] args) {
         String str = "Welcome to java tutorials.";
         System.out.println("Welcome to java tutorials.");
	      // codepoint before index 1 
	      int res = str.codePointBefore(1);

	      // prints character before index1 in string
	      System.out.println("The unicode point is given as = " + res);
	   }
	}

输出:

Welcome to java tutorials.
The unicode point is given as = 87

例子2

public class CharactercodePointBeforeMethodExample2 {
	public static void main(String[] args) {
		 String str = "Welcome to java tutorials.";
        System.out.println("Welcome to java tutorials.");
	      // Codepoint before index 1 
	      int res1 = str.codePointBefore(1);
	      System.out.println( "Unicode code point before the string is " + res1 );
	      System.out.println("The numbers are: '1', '2', '3', '4', '5' ");
	      char[] ch2 = new char[] { '1', '2', '3', '4', '5' };
	      // Codepoint before index 3.
	      int res2 = str.codePointBefore(3);
	      System.out.println( "Unicode code point before the number is " + res2 );
		
	   }
	}

输出:

Welcome to java tutorials.
Unicode code point before the string is 87
The numbers are: '1', '2', '3', '4', '5' 
Unicode code point before the number is 108

例子3

import java.util.Scanner;

public class CharactercodePointBeforeMethodExample3 {
	public static void main(String[] args) {
      // Initialize a new CharSequence object
			CharSequence cs = "'s','t','r','i','n','g'";
	 // Ask for the user to enter the index:
			System.out.print("Enter the desired index:");
			Scanner s = new Scanner(System.in);
			int index = s.nextInt();
			int result = Character.codePointBefore(cs, index);
			System.out.println("Result is given by:"+result);
		}

	}

输出:

Enter the desired index:3
Result is given by:39

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

Character 类的 codePointBefore(char[] a, int index, int start) 方法用于返回 char 数组的给定索引之前的代码点,只有那些索引大于或等于 start 的数组元素才能使用。如果在 char 数组中,(index-1) 处的 char 值在低代理范围内,则 (index-2) 不小于 start。另一方面,如果 (index-2) 处的 char 值在高代理范围内,则返回与代理对对应的补充代码点。否则,它返回 (index-1) 处的 char 值。

用法

public static int codePointBefore(char[] a, int index, int start)

参数

上述方法需要三个参数:

  • 字符数组。
  • 带有应该需要返回的代码点的索引。
  • start 是 char 数组中第一个数组元素的索引。

返回值

codePointBefore(Char[] a, int index, int start) 方法返回索引之前的 Unicode 代码点。

示例 4

public class CharactercodePointBeforeMethodExample4 {
	  public static void main(String[] args) {
            char[] ch = new char[] { 'A', 'b', 'C', 'd'};
            int index  = 3, start = 1;
            int res = Character.codePointBefore(ch, index, start);
	     System.out.println("Welcome to our tutorial site.");
         
           String str = "The unicode code point is " + res;System.out.println( str );
			   }
			}

输出:

Welcome to our tutorial site.
The unicode code point is 67

例 5

public class CharactercodePointBeforeMethodExample5 {    
     public static void main(String[] args)
          {
	        char[] ch1 = new char[] { 'A', 'B', 'C', 'D'};
               int index1  = 3, start1 = 1;
               int res1 = Character.codePointBefore(ch1, index1, start1);
		System.out.println("Welcome to our tutorial site.");
	       System.out.println("The unicode code point for alphabets:");
               String str1 = "The unicode code point is " + res1;
               System.out.println(str1 );
              char[] ch2 = new char[] { '1', '2', '3', '4'};
               int index2  = 3, start2 = 1;
               int res2 = Character.codePointBefore(ch2, index2, start2);
	       System.out.println("The unicode code point for numbers:");
               String str2 = "The unicode code point is " + res2;
              System.out.println(str2 );
                   }
		}

输出:

Welcome to our tutorial site.
The unicode code point for alphabets:
The unicode code point is 67
The unicode code point for numbers:
The unicode code point is 51

例 6

public class CharactercodePointBeforeMethodExample6 {
   public static void main(String[] args)
     {
       System.out.println("The numbers are:'1', '2', '3', '4'");
        char[] ch1 = new char[] { '1', '2', '3', '4'};
	  int index1  = 3, start1 = 1;
         int res1 = Character.codePointBefore(ch1, index1, start1);
         String str1 = "The unicode code point is " + res1;
       System.out.println(str1 );
       System.out.println("The numbers are:'5', '6', '7', '8'");
	  char[] ch2 = new char[] { '5', '6', '7', '8'};
         int index2  = 3, start2 = 1;
         int res2 = Character.codePointBefore(ch2, index2, start2);
          String str2 = "The unicode code point is " + res2;
         System.out.println(str2 );
                }
		}

输出:

The numbers are:'1', '2', '3', '4'
The unicode code point is 51
The numbers are:'5', '6', '7', '8'
The unicode code point is 55

Java Character codePointBefore(charSequence seq, int index) 方法

Character 类的 codePointBefore(charSequence seq, int index) 方法用于返回 charSequence 给定索引之前的代码点。如果在 char 序列中,(index-1) 处的 char 值在低代理范围内,则 (index-2) 不为负。另一方面,如果 (index-2) 处的 char 值在高代理范围内,则返回与此代理对对应的补充代码点。否则,返回 (index-1) 处的 char 值。

用法

public static int codePointBefore(charSequence seq, int index)

参数

上述方法需要两个参数:

  • charSequence 实例。
  • 带有需要返回的代码点的索引。

返回值

codePointBefore(charaSequence seq, int index) 方法返回给定索引之前的 Unicode 代码点值。

例 7

public class CharactercodePointBeforeMethodExample7 {   
     public static void main(String[] args) {
           System.out.println("Welcome to our tutorial site:");
		CharSequence seq1 = "Hello";
		int index1  = 4;
		CharSequence seq2 = "Everyone";
		int index2  = 3;
		int res1 = Character.codePointBefore(seq1, index1);
             String str1 = "The Unicode code point for Java is " + res1;
	     System.out.println( str1 );
		int res2 = Character.codePointBefore(seq2, index2);
             String str2 = "The Unicode code point for Everyone is " + res2;
	     System.out.println( str2 );
		  }
        }

输出:

Welcome to our tutorial site:
The Unicode code point for Java is 108
The Unicode code point for Everyone is 101

例 8

public class CharactercodePointBeforeMethodExample8 {
     public static void main(String[] args) 
       {
	   System.out.println("The string is:Java");
            CharSequence seq1 = "Hello";  
            int index1  = 4;
            int res1 = Character.codePointBefore(seq1, index1);     
            String str1 = "The Unicode code point for Java is " + res1;
          System.out.println( str1 );
          System.out.println("The numbers are:'1', '2', '3', '4'");
            char[] ch2 = new char[] { '1', '2', '3', '4'};
            int index2  = 3;
            int res2 = Character.codePointBefore(ch2, index2);
            String str2 = "The unicode code point for numbers is " + res1;
          System.out.println(str2 );

        }
     }

输出:

The string is:Java
The Unicode code point for Java is 108
The numbers are:'1', '2', '3', '4'
The unicode code point for numbers is 108

例 9

public class CharactercodePointBeforeMethodExample9 {
    public static void main(String[] args) {
        System.out.println("The numbers are:'1', '2', '3', '4':");
           char[] ch1 = new char[] { '1', '2', '3', '4'};
	     int index1  = 3;
	     int res1 = Character.codePointBefore(ch1, index1);
	     String str1 = "The unicode code point for first four numbers is " + res1;
	     System.out.println(str1 );	
        System.out.println("The numbers are:'1', '2', '3', '4':");
            char[] ch2 = new char[] { '5', '6', '7', '8'};
            int index2  = 3;
            int res2 = Character.codePointBefore(ch2, index2);
           String str2 = "The unicode code point for second four numbers is " + res2;
        System.out.println(str2 );

        }
     }

输出:

The numbers are:'1', '2', '3', '4':
The unicode code point for first four numbers is 51
The numbers are:'1', '2', '3', '4':
The unicode code point for second four numbers is 55



相关用法


注:本文由纯净天空筛选整理自 Java Character codePointBefore() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。