Java variable character of self-study notes (StringBuffer)

Overview StringBuffer class

StringBufferIs a string variable, he objects can be expanded and modified.
It is thread-safe, mutable sequence of characters.

The difference between StringBuffer and a String

StringIt is an immutable sequence of characters.
StringBufferIs a variable sequence of characters.

StringBuffer class constructor
public final class StringBuffer extends AbstractStringBuilder implements Appendable, Serializable, CharSequence {
	public StringBuffer(){}//无参构造方法
	public StringBuffer(int capacity){
		//指定容量的字符串缓冲区对象
		super(capacity);
	} 
	public StringBuffer(String str){
		//指定字符串内容的字符串缓冲区对象
		super(str)
	}
}
Use StringBuffer methods

public int capacity(): Returns the current capacity. (Theoretical value: know like)
public int length(): Returns the length (number of characters). (Actual value: often used)

  • Example:
    StringBuffer stringBuffer = new StringBuffer();创建一个StringBuffer对象
    stringBuffer.length();//获取StringBuffer的长度
StringBuffer add functionality
  • public StringBuffer append(String str):
    You can be added to any type of data string buffer inside, and returns the string buffer itself.
  • public StringBuffer insert(int offset,String string):
    At the specified position to insert any kind of data into a string buffer inside, and returns the string buffer itself.
StringBuffer delete function
  • public StringBuffer deleteCharAt(int index):
    Delete the character at the specified location and returns itself.
  • public StringBuffer delete(int start,int end):
    Delete the contents of the specified start position from the end of the specified location and returns itself.
StringBuffer replace and reverse function
  • StringBuffer replace function
    public StringBuffer replace(int start,int end,String str) //从 start 开始到 end 用 str 替换
  • StringBuffer's reverse function
    public StringBuffer reverse() //字符串反转
StringBuffer be intercepted and precautions
  • StringBuffer interception function
    public String substring(int start) //从指定位置截取到末尾
    public String substring(int start,int end) //截取从指定位置开始到结束位置,包括开始位置,不包括结束位置
  • Note: The return value type is no longer StringBuffer itself.
StringBuffer and String interconversion
  • = String> the StringBuffer
    1. By constructing method;
    2. append()Method.
  • = The StringBuffer> String
    1. By constructing method;
    2. toString(); Method
    3. through subString(0,length).
The array translated into strings
  • Demand: the array of data together into a string in the specified format
    public static void main(String[] args){
    	String[] strings = { "a", "b", "c", "d" };
    	StringBuffer stringBuffer = new StringBuffer();
    	for (String string : strings){
    		stringBuffer.append(string);
    	} 
    	System.out.println(stringBuffer.toString());
    }
    
The difference between StringBuffer and StringBuilder
  1. StringBuffer String variable (thread-safe)
  2. StringBuilderString variable (non-thread-safe)
    java.lang.StringBuildera variable sequence of characters is the new 5.0. This class provides with a StringBuffercompatible API, but does not guarantee synchronization. The class is designed as StringBuffera drop-in replacement string buffer is used when using a single thread (which is common). If possible, we recommend the use of such priority, because in most implementations, it is more than StringBufferfast to. Both methods are substantially the same.

Hoping to help people in need.

Released seven original articles · won praise 4 · Views 204

Guess you like

Origin blog.csdn.net/weixin_43712786/article/details/104721982