StringBuilder class usage analysis

1. StringBuilder description

StringBuilder is a mutable sequence of characters. This class provides an API compatible with StringBuffer, but does not guarantee synchronization, i.e. StringBuilder is not thread-safe, while StringBuffer is thread-safe. Obviously, StringBuilder should run faster.

This class is designed to be used by a single thread as a temporary replacement for StringBuffer in place of a StringBuffer (which is usually the case). Where possible, it is recommended to use this class in preference to StringBuffer as it will be faster in most implementations.

2. Characteristics of StringBuilder

(1) This class inherits AbstractStringBuilder and implements java.io.Serializable and CharSequence.

(2) is a string buffer, which can be understood as a container.

(3) Multiple types of data can be stored, but they will eventually be converted to String type through the String.valueof() method and then stored in StringBuilder.

(4) In its parameterless structure, the initial length is 16. In the parameterized structure, the length can be customized according to the business scenario. Customize the length as much as possible to reduce resource consumption for expansion.

(5) It is thread-unsafe, but in terms of speed: StringBuilder (variable, thread-unsafe) > StringBuffer (variable, thread-safe) > String (immutable, safe).

(6) It has natural addition, deletion, modification and query methods such as append(), insert(), deletecharAt(), delete(), reverse(), replace(), etc.

(7) It can be converted to a String type through the toString() method, which returns a string copy of the String type.

3. Common methods of StringBuilder

(1) Parameterless constructor

Copy code

public StringBuilder() {
        super(16); //Call the constructor of the parent class and initialize the length to 16 by default
}

Among them, super(16) means:
AbstractStringBuilder(int capacity) {value = new char[capacity]; // Initialize a variable array 
    of char type with a length of 16
}

Copy code

(2) Structure with parameters (two types)

Copy code

public StringBuilder(String str) {
        super(str.length() + 16); // Usually the constructor of the parent class is called, but the parameter is the length of the incoming string + 16
        append(str); // Add the input parameter str to stringbuilder
    }

public StringBuilder(CharSequence seq) { // CharSequence is an interface that describes the structure of a string. There are three commonly used subclasses in this interface: String, StringBuffer, StringBuilder
        this(seq.length() + 16);
        append(seq);
    }

Copy code

(3)Add method

Copy code

public StringBuilder append(StringBuffer sb) {
        super.append(sb);
        return this;
    }

@Override
public StringBuilder append(CharSequence s) {
        super.append(s);
        return this;
    }
   
@Override
public StringBuilder append(CharSequence s, int start, int end) {
        super.append(s, start, end);
        return this;
    }

@Override
public StringBuilder append(char[] str) {
        super.append(str);
        return this;
    }


@Override
public StringBuilder append(char[] str, int offset, int len) {
        super.append(str, offset, len);
        return this;
    }

@Override
public StringBuilder append(boolean b) { //The input parameters here can also be replaced with char, int, float, long, double
        super.append(b);
        return this;
    }

Copy code

(4)Deletion method

Copy code

@Override
public StringBuilder delete(int start, int end) {
        super.delete(start, end); // Delete the string in the range [start, end)
        return this;
    }

@Override
public StringBuilder deleteCharAt(int index) { // Delete the string at the specified index position
        super.deleteCharAt(index);
        return this;
    }

Copy code

(5) Insertion (modification) method

Copy code

@Override
public StringBuilder insert(int index, char[] str, int offset, int len){
        super.insert(index, str, offset, len);
        return this;
    }

    
@Override
public StringBuilder insert(int offset, Object obj) {
        super.insert(offset, obj);
        return this;
    }

  
@Override
public StringBuilder insert(int offset, String str) {
        super.insert(offset, str);
        return this;
    }

@Override
public StringBuilder insert(int offset, char[] str) {
        super.insert(offset, str);
        return this;
    }


@Override
public StringBuilder insert(int dstOffset, CharSequence s) {
        super.insert(dstOffset, s);
        return this;
    }

@Override
public StringBuilder insert(int dstOffset, CharSequence s, int start, int end){
        super.insert(dstOffset, s, start, end);
        return this;
    }


@Override
public StringBuilder insert(int offset, boolean b) { // The second input parameter here can also be replaced with char, int, float, long, double
        super.insert(offset, b);
        return this;
    }
@Override
public StringBuilder replace(int start, int end, String str) { // Replacement method
    super.replace(start, end, str);
    return this;
}

Copy code

(6) Query method (similar to the method in String that queries the index position of the first/last occurrence of string str starting from a certain index position 0/fromIndex)

Copy code

@Override
public int indexOf(String str) {
        return super.indexOf(str);
    }

@Override
public int indexOf(String str, int fromIndex) {
        return super.indexOf(str, fromIndex);
    }

@Override
public int lastIndexOf(String str) {
        return super.lastIndexOf(str);
    }

@Override
public int lastIndexOf(String str, int fromIndex) {
        return super.lastIndexOf(str, fromIndex);
    }

Copy code

(7)Reverse StringBuilder

@Override
public StringBuilder reverse() {
        super.reverse();
        return this;
    }

(8) toString() method

@Override
public String toString() {
        // Create a copy, don't share the array
        return new String(value, 0, count); // Returns a new String type string
    }

Guess you like

Origin blog.csdn.net/Gefangenes/article/details/132483894