StringBuffer類的toString()方法是一種內置方法,用於返回表示StringBuffer Object包含的數據的字符串。創建並初始化一個新的String對象,以從該StringBuffer對象獲取字符序列,然後由toString()返回String。 Object包含的對該序列的後續更改不會影響String的內容。
用法:
public abstract String toString()
返回值:此方法返回表示StringBuffer Object包含的數據的String。
以下示例程序旨在說明StringBuffer.toString()方法:
示例1:
// Java program to demonstrate
// the toString() Method.
class GFG {
public static void main(String[] args)
{
// create a StringBuffer object
// with a String pass as parameter
StringBuffer str
= new StringBuffer("GeeksForGeeks");
// print string
System.out.println("String contains = "
+ str.toString());
}
}
輸出:
String contains = GeeksForGeeks
示例2:
// Java program to demonstrate
// the toString() Method.
class GFG {
public static void main(String[] args)
{
// create a StringBuffer object
// with a String pass as parameter
StringBuffer str
= new StringBuffer(
"Geeks for Geeks contribute");
// print string
System.out.println("String contains = "
+ str.toString());
}
}
輸出:
String contains = Geeks for Geeks contribute
參考文獻:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuffer.html#toString()
相關用法
- Java StringBuffer trimToSize()用法及代碼示例
- Java StringBuffer getChars()用法及代碼示例
- Java StringBuffer setCharAt()用法及代碼示例
- Java StringBuffer indexOf()用法及代碼示例
- Java StringBuffer codePointCount()用法及代碼示例
- Java StringBuffer codePointAt()用法及代碼示例
- Java StringBuffer ensureCapacity()用法及代碼示例
- Java StringBuffer offsetByCodePoints()用法及代碼示例
- Java StringBuffer substring()用法及代碼示例
- Java StringBuffer replace()用法及代碼示例
- Java StringBuffer deleteCharAt()用法及代碼示例
- Java StringBuffer codePointBefore()用法及代碼示例
- Java StringBuffer append()用法及代碼示例
- Java StringBuffer appendCodePoint()用法及代碼示例
- Java StringBuffer delete()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 StringBuffer toString() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。