“This is the 18th day of my participation in the August Gwen Challenge.

The StringBuilder class

How the StringBuilder class works

A StringBuilder is called a character buffer and can be used to manipulate strings more efficiently (it can be thought of as a string of variable length). The underlying string is also an array, but there is no final modifier and the length can be changed.

byte[] value = new byte[16]

The initial size of the underlying array of the StringBuilder class is 16, and if it grows out of capacity, StringBuilder automatically expands it.

StrigBuilder class constructor

Public StringBuilder(String STR) Constructs a String generator and initializes it to the specified String content. The initial capacity of the string generator is 16 plus the length of the string arguments. Parameter: STR - The initial contents of the buffer. Public StringBuilder() constructs a string generator without any characters, with an initial capacity of 16 characters. Public StringBuilder(int capacity) Constructs a string generator without any characters whose initial capacity is specified by the capacity parameter. Public StringBuilder(CharSequence seq) Constructs a string generator that contains the same characters as the specified CharSequence. The initial capacity of this string generator is 16 plus the length of the CharSequence argument. Argument: seq - The sequence to copy.Copy the code

The StringBuilder class has four constructors, and we’ll focus on the first two.

Member methods of StringBuilder

The main operations on StringBuilder are the Append and INSERT methods, which can be overridden to accept arbitrary types of data. Each method effectively converts the given data to a string, and then appends or inserts the characters of that string into the string generator. The append method always adds these characters to the end of the generator; The INSERT method adds characters at the specified point.

Append () method

@Override
public StringBuilder append(String str) {
    super.append(str);
    return this;
}
Copy the code

Append has many overloaded methods that can add strings of any type and return the current object itself.

StringBuilder sb = new StringBuilder(); sb.append("abc"); sb.append(1); Sb. Append (3.3); sb.append('d'); sb.append(true); System.out.println(sb);Copy the code

In the case of AppEnd, it returns itself as an object, so you can use chained programming to achieve this effect, and it’s much simpler.

StringBuilder sb = new StringBuilder(); Sb. Append (" ABC "). Append (1), append (3.3), append (' d '). Append (true); System.out.println(sb);Copy the code

Insert () method

Public StringBuilder Insert (int offset,String STR) Inserts a String into the character sequence. Inserts the characters in the String argument sequentially into the specified position in this sequence, pushing back the original characters at that position, and this sequence increases the length of the argument. If STR is null, four characters "null" are appended to the sequence.Copy the code

The insert method is similar to the append method, but with an extra parameter to the index, which is not covered here.

The toString () method

StringBuilder and String can convert to each other.

String->StringBuilder, via the constructor of StringBuilder.

StringBuilder(String str)
Copy the code

StringBuilder->String via the toString() method of StringBuilder.

Public String toString() returns a String representation of the data in this sequence. A new String object is allocated and initialized to contain the sequence of strings currently represented by this object. Then return the String. Subsequent changes to this sequence do not affect the contents of the String.Copy the code

Write in the last

Ok, so that’s the StringBuilder class. A class that has similar functionality to StringBuilder is called StringBuffer. The difference is that StringBuilder doesn’t support multithreading, it’s faster, So StringBuilder is preferred in most cases, and The StringBuffer class supports multithreading and is more secure. You can check out the API documentation.

If the above content is not correct, welcome to dig friends to criticize.