Common Java classes common methods of StringBuilder and StringBuffer classes in java.lang package

Article directory


Last article Common Java classes common methods of java.lang package (6) String class The common method of String is described in, but String is immutable. If you need to operate a large number of character splicing, it takes up too much memory space.

Now let's talk about the common methods of variable character sequence, StringBuilder and StringBuffer.

Because these two class methods and use are basically the same, only in the multi-threaded environment safe and unsafe. So this article uses the StringBuilder example.

I. outline

A variable sequence of characters. This class provides an API that is compatible with StringBuffer, but does not guarantee synchronization. This class is designed to be a simple replacement for StringBuffer when it is used by a single thread (this is common). If possible, this class is preferred because in most implementations it is faster than StringBuffer.

public final class StringBuilder extends Object implements Serializable, CharSequence

Two, use

1. constructor

1.1 create

He has different constructors for creating a variable character sequence StringBuilder.

        StringBuilder sb01 = new StringBuilder(); // Create an uninitialized StringBuilder
        StringBuilder sb02 = new StringBuilder("Initialization init"); // Create a StringBuilder with an initialization string
        StringBuilder sb03 = new StringBuilder(20); // Create a StringBuilder with specified initialization capacity

2. Common methods

2.1 get

The following methods can get information about this StringBuilder.

For example: capacity, length, location where the lookup character or string appears.

        StringBuilder sb = new StringBuilder("hello");
        // Capacity acquisition
        int c = sb.capacity();
        System.out.println(c); // 21

        // Get the length of the string
        int len = sb.length();
        System.out.println(len); // 5

        // Gets the first occurrence of a character or string (from scratch, or from a specified location)
        int i = sb.indexOf("e"); // Start from scratch
        System.out.println(i); // 1
        int i1 = sb.indexOf("e", 1); // Start at the specified location
        System.out.println(i1); // 1

        // Gets the first occurrence of a character or string (starting at the end, or at the specified end)
        int li = sb.lastIndexOf("e"); // From the tail
        System.out.println(li); // 1
        int li1 = sb.lastIndexOf("e", 5); // Start at specified tail position
        System.out.println(li1); // 1
        // Gets the char value at the specified index
        char ca = sb.charAt(0);
        System.out.println(ca); // h
2.2 additional

append(String str)
Appends the specified string to this character sequence.

You can add values of multiple data types to StringBuilder.

        StringBuilder sb = new StringBuilder();
        sb.append("hello ");
        System.out.println(sb); // hello 123
2.3 insert + remove

insert(int offset, String str)
Inserts a string into this character sequence.

This method can insert values of various data types into StringBuilder.

delete(int start, int end)
Remove characters from the substring of this sequence.
deleteCharAt(int index)
Remove the char at the specified location of this sequence.

These two methods can remove the character or string at the specified location.

        StringBuilder sb = new StringBuilder("hello");
        // insert
        sb.insert(sb.length(), "?"); // Insert one at the end?
        sb.insert(0, '-'); // Insert a-
        System.out.println(sb); // -hello?

        // remove
        sb.delete(sb.length()-1, sb.length()); // Remove tail?
        sb.deleteCharAt(0); // Removes characters from the specified index. Remove the first-
        System.out.println(sb); // hello
2.4 replacement (character)

setCharAt(int index, char ch)
Set the character at the given index to ch.

Sets the character at the specified index to the new character.

        StringBuilder sb = new StringBuilder("hello");
        sb.setCharAt(0, 'H');
        System.out.println(sb); // Hello
2.5 substitution (string)

replace(int start, int end, String str)
Replace the characters in the substring of this sequence with the characters in the given String.

From the specified position to the specified position, replace with another string.

        StringBuilder sb = new StringBuilder("hello xxxxx!");

        String s = "world";
        // Replace the string s from the position with index 6 to the position with index 11.
        sb.replace(6, 11, s);
        System.out.println(sb); // hello world!
2.6 reverse string

Replace this character sequence with its reverse form.

That is to flip the string in StringBuilder. This method is quite common.

        StringBuilder sb = new StringBuilder("hello");
        System.out.println(sb); // olleh
2.7 get substring

substring(int start, int end) ,substring(int start)
Returns a new String containing a subsequence of the characters currently contained in the sequence.

Gets the substring of the String from the specified location. Same as String.

        StringBuilder sb = new StringBuilder("hello");
        String sub = sb.substring(2);
        System.out.println(sub); // llo

        String sub02 = sb.substring(2, 5);
        System.out.println(sub02); // llo


StringBuilder is the same as StringBuffer class. You can learn any other one.

Several of their methods are the same as the String class. After learning the String class, they are almost bypassed.


For more common classes, see: [Java common classes] directory

Published 110 original articles, praised 127, visited 20000+
Private letter follow

Keywords: Java

Added by kenshintomoe225 on Wed, 29 Jan 2020 14:31:04 +0200