StringBuilder類的toString()方法是一種內置方法,用於返回表示StringBuilder對象包含的數據的字符串。創建並初始化一個新的String對象,以從該StringBuilder對象獲取字符序列,然後由toString()返回String。 Object包含的對該序列的後續更改不會影響String的內容。
用法:
public abstract String toString()
返回值:此方法返回表示StringBuilder對象包含的數據的String。
以下示例程序旨在說明StringBuilder.toString()方法:
示例1:
// Java program to demonstrate
// the toString() Method.
class GFG {
public static void main(String[] args)
{
// create a StringBuilder object
// with a String pass as parameter
StringBuilder str
= new StringBuilder("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 StringBuilder object
// with a String pass as parameter
StringBuilder str
= new StringBuilder(
"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/StringBuilder.html#toString()
相關用法
- Java StringBuilder offsetByCodePoints()用法及代碼示例
- Java StringBuilder append()用法及代碼示例
- Java StringBuilder substring()用法及代碼示例
- Java StringBuilder lastIndexOf()用法及代碼示例
- Java StringBuilder appendCodePoint()用法及代碼示例
- Java StringBuilder indexOf()用法及代碼示例
- Java StringBuilder trimToSize()用法及代碼示例
- Java Matcher appendTail(StringBuilder)用法及代碼示例
- Java Matcher appendReplacement(StringBuilder, String)用法及代碼示例
- Java DecimalFormatSymbols toString()用法及代碼示例
- Java LongAdder toString()用法及代碼示例
- Java AtomicLongArray toString()用法及代碼示例
- Java Signature toString()用法及代碼示例
- Java CharArrayWriter toString()用法及代碼示例
- Java StringBuffer toString()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 StringBuilder toString() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。