此方法有 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 codePointCount()用法及代碼示例
- Java Character codePointAt()用法及代碼示例
- Java Character compare()用法及代碼示例
- Java Character charCount()用法及代碼示例
- Java Character isLetter()用法及代碼示例
- Java Character isAlphabetic()用法及代碼示例
- Java Character isValidCodePoint()用法及代碼示例
- Java Character isISOControl()用法及代碼示例
- Java Character getType()用法及代碼示例
- Java Character getNumericValue()用法及代碼示例
- Java Character isSpace()用法及代碼示例
- Java Character toTitleCase()用法及代碼示例
- Java Character isMirrored()用法及代碼示例
- Java Character isBmpCodePoint()用法及代碼示例
- Java Character toUpperCase()用法及代碼示例
- Java Character isIdentifierIgnorable()用法及代碼示例
- Java Character isDigit()用法及代碼示例
- Java Character digit()用法及代碼示例
- Java Character toChars()用法及代碼示例
- Java Character isLowerCase()用法及代碼示例
注:本文由純淨天空篩選整理自 Java Character codePointBefore() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。