StringBuffer類的offsetByCodePoints()方法返回StringBuffer包含的此String中的索引,該索引與codePointOffset代碼點作為參數傳遞的索引偏移。未配對的替代項位於index和codePointOffset計數之間,每個代表一個代碼點。
用法:
public int offsetByCodePoints(int index, int codePointOffset)
參數:此方法有兩個參數:
- index:要偏移的索引
- codePointOffset:代碼點的偏移量
返回值:此方法返回此序列內的索引。
異常:如果以下任何一項為真,則此方法將引發IndexOutOfBoundsException:
- 序列的索引長度。
- codePointOffset> 0,以index開頭的子序列少於codePointOffset代碼點
- codePointOffset
下麵的程序演示StringBuffer類的offsetByCodePoints()方法:
示例1:
// Java program to demonstrate
// the offsetByCodePoints() Method.
class GFG {
public static void main(String[] args)
{
// create a StringBuffer object
// with a String pass as parameter
StringBuffer
str
= new StringBuffer("WelcomeGeeks");
// print string
System.out.println("String = "
+ str.toString());
// returns the index within this sequence
int returnvalue = str.offsetByCodePoints(1, 4);
// prints the index
System.out.println("Index = " + returnvalue);
}
}
輸出:
String = WelcomeGeeks Index = 5
示例2:
// Java program to demonstrate
// the offsetByCodePoints() Method.
class GFG {
public static void main(String[] args)
{
// create a StringBuffer object
// with a String pass as parameter
StringBuffer
str
= new StringBuffer("India Is great");
// print string
System.out.println("String = " + str.toString());
// returns the index within this sequence
int returnvalue = str.offsetByCodePoints(2, 9);
// prints the index
System.out.println("Index = " + returnvalue);
}
}
輸出:
String = India Is great Index = 11
示例3:演示IndexOutOfBoundException
// Java program to demonstrate
// Exception thrown by offsetByCodePoints() Method.
class GFG {
public static void main(String[] args)
{
// create a StringBuffer object
// with a String pass as parameter
StringBuffer
str
= new StringBuffer("India");
try {
// returns the index within this sequence
int returnvalue = str.offsetByCodePoints(2, 9);
// prints the index
System.out.println("Index = " + returnvalue);
}
catch (IndexOutOfBoundsException e) {
System.out.println("Exception: " + e);
}
}
}
輸出:
Exception: java.lang.IndexOutOfBoundsException
參考:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuffer.html#offsetByCodePoints(int, int)
相關用法
- Java StringBuilder offsetByCodePoints()用法及代碼示例
- Java StringBuffer ensureCapacity()用法及代碼示例
- Java StringBuffer codePointAt()用法及代碼示例
- Java StringBuffer toString()用法及代碼示例
- Java StringBuffer trimToSize()用法及代碼示例
- Java StringBuffer setCharAt()用法及代碼示例
- Java StringBuffer lastIndexOf()用法及代碼示例
- Java StringBuffer substring()用法及代碼示例
- Java StringBuffer getChars()用法及代碼示例
- Java StringBuffer codePointBefore()用法及代碼示例
- Java StringBuffer codePointCount()用法及代碼示例
- Java StringBuffer delete()用法及代碼示例
- Java StringBuffer reverse()用法及代碼示例
- Java StringBuffer replace()用法及代碼示例
- Java StringBuffer deleteCharAt()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 StringBuffer offsetByCodePoints() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。