W3schools Java StringBuffer Tutorial Guide

Robert Jul 13, 2026

W3Schools, a popular resource for learning web development, offers a comprehensive tutorial on Java's StringBuffer class. If you're new to Java or need a refresher, this guide provides an excellent starting point.

Java script string method
Java script string method

The StringBuffer class in Java is used to create mutable (changeable) strings. It's well-suited for situations where you might be doing frequent appends and modifications to strings. Let's dive into the key features and methods provided by the StringBuffer class.

Strings in Java | Difference Between String, StringBuffer & StringBuilder Explained
Strings in Java | Difference Between String, StringBuffer & StringBuilder Explained

Creating and Initializing StringBuffer

The StringBuffer class has several constructors that allow you to create and initialize objects differently. The two most common are:

String Methods in Java Explained | Java Programming Basics
String Methods in Java Explained | Java Programming Basics

StringBuffer(): Creates a StringBuffer with no characters. Its initial capacity is 16 characters.

StringBuffer(String str): Creates a StringBuffer initialized with the passed string, with a capacity of at least 16 characters plus the length of the string.

Java Programming CheatsheetIntro to Java Programming: An Interdisciplinary Approach and Computer Science: An Interdisciplinary Approach by Sedgewick and Wayne
Java Programming CheatsheetIntro to Java Programming: An Interdisciplinary Approach and Computer Science: An Interdisciplinary Approach by Sedgewick and Wayne

Appending to a StringBuffer

The StringBuffer class provides several append() methods that allow you to add content to the buffer. These methods return the modified StringBuffer, allowing for chaining of method calls.

Here are a few examples:

java programs
java programs
  • sb.append("Hello"); adds "Hello" to the end of the buffer.
  • sb.append(25); appends the integer 25 to the buffer. It's converted to a string first.
  • sb.append(true); appends the boolean value "true" to the buffer.

获取和修改字符串

StringBuffer提供了几种方法来获取和修改缓冲区的字符串:

two different types of words that appear to be in the same language, one is for each
two different types of words that appear to be in the same language, one is for each
  • sb.toString() 返回此 StringBuffer 的字符串表示形式。
  • sb.setCharAt(int index, char ch) 将指定索引处的字符更改为指定的Character提供的字符。
  • sb.replace(int start, int end, String str) 用指定的 String 替换从指定的开始까지 (不包括结束处) 的子字符串。

操作字符串中的子字符串

Java Bootcamp London & UK – Part Time Java Developer Program
Java Bootcamp London & UK – Part Time Java Developer Program
Java Patterns Notes 1
Java Patterns Notes 1
Everything about String Vs String Builder Vs String Buffer
Everything about String Vs String Builder Vs String Buffer
a piece of paper with writing on it that says string method in jaascript
a piece of paper with writing on it that says string method in jaascript
Methods Of String Class In Java With Example
Methods Of String Class In Java With Example
Strings in Java Explained | Java Programming Basics for Beginners
Strings in Java Explained | Java Programming Basics for Beginners
[Solved] How to count Vowels and Consonants in Java String Word? Example
[Solved] How to count Vowels and Consonants in Java String Word? Example
BufferedByteStream - Java | Geekboots
BufferedByteStream - Java | Geekboots
String concatenation using StringBuilder class
String concatenation using StringBuilder class
Wrapper Classes in Java | Autoboxing & Unboxing Explained
Wrapper Classes in Java | Autoboxing & Unboxing Explained
Multithreading in Java Explained | Thread Life Cycle & Creation Methods
Multithreading in Java Explained | Thread Life Cycle & Creation Methods
🚀 MULTITHREADING IN JAVA MADE EASY! ⚡
🚀 MULTITHREADING IN JAVA MADE EASY! ⚡
Java Thread | Multithreading Basics
Java Thread | Multithreading Basics
java for complete beginners - more on java strings
java for complete beginners - more on java strings
Java String equals() Method
Java String equals() Method
Java String | technical-arbaab
Java String | technical-arbaab
Learn Java in 30 Days Only
Learn Java in 30 Days Only
How to Reverse A String in JavaScript
How to Reverse A String in JavaScript
Stringbuffer vs String builder
Stringbuffer vs String builder
Java Variables Explained | Local, Static & Instance Variables in Java
Java Variables Explained | Local, Static & Instance Variables in Java

StringBuffer还提供了对缓冲区中字符串的插入、删除和替换操作:

插入: You can use the insert(int offset, String str) method to insert a string at the specified offset.

删除: You can use the delete(int start, int end) method to remove characters from the buffer. It removes the substring from the specified start to end.

替换: The deleteCharAt(int index) method removes the character at the specified position.

转换为String

When you're done working with a StringBuffer, you can convert it back to a String using:

String str = sb.toString();

To smoothly transition to your next task, practice using the StringBuffer class with the examples provided in the W3Schools tutorial, and don't hesitate to explore other Java classes that work with strings. Happy coding!