StringBuffer class
1. Overview
StringBuffer is a thread-safe mutable string
What is the difference between 2.StringBuffer and string?
StringBuffer the length and content of the former are variable, the latter is immutable.
If you use the former to do string concatenation, it will not waste too much resources.
How to construct 3.StringBuffer
Public StringBuffer (): Non-parametric construction method
public stringbuffer (int capacity): Specifies the capacity of the string buffer object
Public StringBuffer (String str): String buffer object specifying string contents
4.StringBuffer Common methods
(1). Length method (or Capacity method)
public int Capacity (): Returns the current capacity. Theoretical value
public int Length (): Returns the length (number of characters). Actual value
Examples of Use:
public static void Main (string[] args) {
StringBuffer sb=new StringBuffer ();
SYSTEM.OUT.PRINTLN ("---" +SB);
System.out.println ("Sb.capacity ():" +sb.capacity ());
System.out.println ("Sb.length ():" +sb.length ());
System.out.println ("-----------------------");
StringBuffer sb1=new stringbuffer ("HelloWorld");
SYSTEM.OUT.PRINTLN ("---" +sb1);
System.out.println ("Sb.capacity ():" +sb1.capacity ());
System.out.println ("Sb.length ():" +sb1.length ());
}
Output Result:
---
Sb.capacity (): 16
Sb.length (): 0
-----------------------
---HelloWorld
Sb.capacity (): 26 (16+10)
Sb.length (): 10
(2). Add method
Public stringbuffer append (String str): You can add any type of data to the string buffer and return the buffer itself.
Public stringbuffer Insert (int offset,string str): Inserts any type of data into the string buffer at the specified location and returns the buffer itself.
Use an example of
public static void Main (string[] args) {
StringBuffer sb=new stringbuffer ();
Sb.append ("Hello"). Append ("the World"). Append ("---"). Append ("Hello"). Append ("Worlds");
System.out.println (SB);
System.out.println ("--------------");
Sb.insert (2, "test");
System.out.println (SB);
}
//Output
Hello World---helloworld
--------------
Hello Test World---helloworld
(3). Delete function
public stringbuffer deletechartat (int index): Deletes the character at the specified position and returns the string buffer after deletion.
Public StringBuffer Delete (int start,int end): Deletes from the specified position to the specified position, returning the string itself.
Use an example of
public static void Main (string[] args) {
StringBuffer sb=new stringbuffer ("HelloWorld");
Sb.deletecharat (2);
System.out.println (SB);
System.out.println ("--------------");
Sb.delete (0, 3);
System.out.println (SB);
}
//output result
Heloworld
--------------
Oworld
(4). Replace function
Public stringbuffer replace (int strat,int end,string str): From the specified position start to end using the STR string to replace, the package left does not wrap to the right.
Examples of Use:
public static void Main (string[] args) {
StringBuffer sb=new stringbuffer ("HelloWorld");
String str= "123";
Sb.replace (1, 3, str);
System.out.println (SB);
}
Output results
H123loworld
(5). Invert function
Public StringBuffer reverse (): Outputs the original string backwards
Examples of Use:
public static void Main (string[] args) {
StringBuffer sb=new stringbuffer ("HelloWorld");
System.out.println (SB);
System.out.println ("------------------------");
Sb.reverse ();
System.out.println (SB);
}
Output results
HelloWorld
------------------------
Dlrowolleh
(6). StringBuffer interception function (no longer returns itself, but returns the intercepted content)
public string substring (int start), starting at the specified position, and re-assigning the intercepted string to a string
public string substring (int start,int end), starting from the specified position to the last intercept, and re-assigning the intercepted string to a string
Examples of Use
public static void Main (string[] args) {
StringBuffer sb=new stringbuffer ("HelloWorld");
String str=sb.substring (3);
System.out.println (str);
System.out.println ("----------------------");
String str1=sb.substring (1,3);
System.out.println (STR1);
}
Output results
Loworld
----------------------
El
5.StringBuffer of related operations
(1). The mutual transformation between string and StringBuffer
Why we need to explain the transformation between classes
String-->stringbuffer
We can use the StringBuffer function
But what we need is the string class, so
Stringbuffer-->string
Code:
public static void Main (string[] args) {
String str = "HelloWorld";
String-->stringbuffer
by constructing
StringBuffer SB =new stringbuffer (str);
Through the Apends () method
StringBuffer sb1=new StringBuffer ();
Sb1.append (str);
System.out.println ("SB:" +SB);
System.out.println ("SB1:" +SB1);
System.out.println ("-----------------");
StringBuffer--->string
StringBuffer buffer =new stringbuffer ("Java");
Constructed from string
String Str1=new string (buffer);
The ToString () method of the string
String str2=buffer.tostring ();
System.out.println ("str1:" +STR1);
System.out.println ("str2:" +STR2);
}
Output results
Sb:helloworld
Sb1:helloworld
-----------------
Str1:java
Str2:java
6. Related questions
(1). The difference between String,stringbuffer,stringbuilder
The contents of the string are immutable, while the contents of the Stringbuffer,stringbuilder are mutable
The StringBuffer is synchronous, the data is safe, the efficiency is low, the stringbilder is not synchronized, the data is unsafe, and the efficiency is high.
(2). What is the difference between a stringbuffer and an array?
StringBuffer and arrays can be seen as a container for reproducing data
However, the StringBuffer data is ultimately a string data (the data type can be different).
Arrays can place multiple data, but they must be of the same data type.
(3). Formal parameter issues (*)
String is passed as a parameter
StringBuffer passed as a parameter
Formal parameters:
Basic type: Changes in formal parameters do not affect actual parameters
Reference type: Changes in formal parameters directly affect actual parameters
Java Api_stringbuffer Class