此方法有兩種類型的語法。
Java 字符 codePointCount(char[]a, int offset, int count) 方法
codePointCount(char[]a, int offset, int count) 方法用於返回char數組的子數組中Unicode碼點的個數。 offset 參數是給定 char 數組中第一個 char 的索引,count 參數表示子數組的長度。
用法
public static int codePointCount(char[] a, int offset, int count)
參數
上述方法需要三個參數:
- 字符數組。
- 偏移量是給定字符數組中第一個字符的索引。
- 計數是給定字符中子數組的長度。
返回值
此方法返回指定子數組中 Unicode 代碼點的數量。
例子1
public class CharactercodePointCountMethodExample1 {
public static void main(String[] args) {
System.out.println("Welcome to our tutorial site:");
CharSequence seq = "Hello";
// Declare offset.
int offset = 2;
// Declare count.
int countValue = 4;
int result = Character.codePointCount(seq, offset, countValue);
// Print the result.
System.out.println("The result is given as:" + result);
}
}
輸出:
Welcome to our tutorial site: The result is given as:2
例子2
public class CharactercodePointCountMethodExample2 {
public static void main(String[] args) {
System.out.println("The alphabets are:'a', 'b', 'c', 'd', 'e'");
char[] c1 = new char[] { 'a', 'b', 'c', 'd', 'e' };
int offset1 = 0, count1 = 4;
int res1 = Character.codePointCount(c1, offset1, count1);
System.out.println("The number of Unicode code point for alphabets are " + res1);
System.out.println("The numbers are: '1', '2', '3', '4', '5'");
char[] c2 = new char[] { '1', '2', '3', '4', '5' };
int offset2 = 1, count2 = 3;
int res2 = Character.codePointCount(c2, offset2, count2);
System.out.println("The number of Unicode code point for numbers are " + res2);
}
}
輸出:
The alphabets are:'a', 'b', 'c', 'd', 'e' The number of Unicode code point for alphabets are 4 The numbers are: '1', '2', '3', '4', '5' The number of Unicode code point for numbers are 3
例子3
public class CharactercodePointCountMethodExample3{
public static void main(String args[])
{
String str1 = "Hello everyone";
System.out.println("Hello everyone");
int Total1 = str1.codePointCount(3, 9);
System.out.println(Total1 + " is generated because method counts the first argument.");
String str2 = "Everday is a fun day";
System.out.println("Everyday is a fun day");
int Total2 = str2.codePointCount(6, 8);
System.out.println(Total2 + " is generated because method counts the second argument.");
}
}
輸出:
Hello everyone 6 is generated because method counts the first argument. Everyday is a fun day 2 is generated because method counts the second argument.
Java 字符 codePointCount()Method
Character 類的 codePointCount(CharSequence seq, int beginIndex, int endIndex) 方法用於返回特定字符序列的文本範圍內的 Unicode 代碼點數。文本範圍從 beginIndex 開始,到 endIndex 結束。
用法
Public static int codePointCount(CharSequence seq, int beginIndex, int endIndex)
參數
上述方法需要三個參數:
- 字符序列。
- beginIndex 是文本範圍的第一個字符的索引。
- endIndex 是文本範圍最後一個字符之後的索引。
返回值
codePointCount(CharSequence seq, int beginIndex, int endIndex) 方法返回指定文本範圍內的 Unicode 代碼點數。
示例 4
public class CharactercodePointCountMethodExample4 {
public static void main(String[] args) {
// Initialize a new CharSequence object
CharSequence cs = "This is a unique example!";
System.out.println("This is a unique example!");
// Initialize beginIndex and endIndex
int beginIndex = 0;
int endIndex = cs.length();
int result = Character.codePointCount(cs, beginIndex,endIndex);
System.out.println("The result comes to be:"+result);
}
}
輸出:
This is a unique example! The result comes to be:25
例 5
public class CharactercodePointCountMethodExample5 {
public static void main(String[] args) { // Initialize a new CharSequence object
CharSequence cs1 = "This is a unique example!"; System.out.println("This is a unique example!");
// Initialize beginIndex and endIndex
int beginIndex1 = 0;
int endIndex1 = cs1.length();
int result1 = Character.codePointCount(cs1, beginIndex1,endIndex1);
System.out.println("The result comes to be:"+result1);
CharSequence cs2 = "Hello World!";
System.out.println(cs2);
int beginIndex2 = 4, endIndex2 = 8;
int result2 = Character.codePointCount(cs2, beginIndex2, endIndex2);
String str = "Number of Unicode code points is " + result2;
System.out.println( str );
}
}
輸出:
This is a unique example! The result comes to be:25 Hello World! Number of Unicode code points is 4
例 6
public class CharactercodePointCountMethodExample6 {
public static void main(String[] args) {
// Initialize a new CharSequence object
CharSequence cs1 = "This is a unique example!";
System.out.println(cs1);
// Initialize beginIndex and endIndex
int beginIndex1 = 0;
int endIndex1 = cs1.length();
int result1 = Character.codePointCount(cs1, beginIndex1,endIndex1);
System.out.println("The result comes to be:"+result1);
CharSequence cs2 = "Java is a high level language!";
System.out.println(cs2);
int beginIndex2 = 0;
int endIndex2 = cs2.length();
int result2 = Character.codePointCount(cs2, beginIndex2,endIndex2);
System.out.println("The result comes to be:"+result2);
CharSequence cs3 = "Java is based on the cocepts of OOPS!";
System.out.println(cs3);
int beginIndex3 = 0;
int endIndex3 = cs3.length();
int result3 = Character.codePointCount(cs3, beginIndex3,endIndex3);
System.out.println("The result comes to be:"+result3);
}
}
輸出:
This is a unique example! The result comes to be:25 Java is a high level language! The result comes to be:30 Java is based on the cocepts of OOPS! The result comes to be:37
相關用法
- Java Character codePointAt()用法及代碼示例
- Java Character codePointBefore()用法及代碼示例
- 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 codePointCount() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。