Java classes commonly used method [] java.lang package (7) StringBuilder, StringBuffer class




Foreword

Previous [Java] commonly used class java.lang package (6) commonly used method of the String class I talked about a common method of String, but String is immutable, so take up memory space when you need to manipulate a lot of character stitching.

The variable sequence of characters, StringBuilder, StringBuffer class common method now speak.

Because these two classes using methods are basically the same, just a multithreaded environment secure and non-secure. Therefore, this article will use StringBuilder example.

First, outline

A variable sequence of characters. This class provides a StringBuffer compatible with the API, but does not guarantee synchronization. The class is designed as a drop-in replacement StringBuffer, the 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 faster than StringBuffer.
Here Insert Picture Description

public final class StringBuilder extends Object implements Serializable, CharSequence

Second, the use

1. Constructor

1.1 Creating

He has a different structure is used to create a variable sequence of characters StringBuilder.

        StringBuilder sb01 = new StringBuilder(); // 创建一个未初始化的StringBuilder
        StringBuilder sb02 = new StringBuilder("初始化init"); // 创建一个带有初始化字符串的StringBuilder
        StringBuilder sb03 = new StringBuilder(20); // 创建一个指定初始化容量的StringBuilder

2. The common method

2.1 Gets

The following methods can StringBuilder acquired the relevant information.

Such as: capacity position, length, or to find a character string appears.

        StringBuilder sb = new StringBuilder("hello");
        // 获取容量
        int c = sb.capacity();
        System.out.println(c); // 21

        // 获取该字符串长度
        int len = sb.length();
        System.out.println(len); // 5

        // 获取字符或字符串第一次出现的位置(从头开始,或从指定位置开始)
        int i = sb.indexOf("e"); // 从头开始
        System.out.println(i); // 1
        int i1 = sb.indexOf("e", 1); // 从指定位置开始
        System.out.println(i1); // 1

        // 获取字符或字符串第一次出现的位置(从尾部开始,或从指定尾部位置开始)
        int li = sb.lastIndexOf("e"); // 从尾部开始
        System.out.println(li); // 1
        int li1 = sb.lastIndexOf("e", 5); // 从指定尾部位置开始
        System.out.println(li1); // 1
        
        // 获取指定索引处的char值
        char ca = sb.charAt(0);
        System.out.println(ca); // h
2.2 Additional

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

You may be various types of binary data, added to the StringBuilder.

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

insert (int offset, String str)
to insert a string of characters in this sequence.

The method may be multiple types of data values, is inserted into the StringBuilder.

delete (int start, int end)
to remove this sequence substring of characters.
deleteCharAt (int index)
to remove the char at the specified position in this sequence.

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

        StringBuilder sb = new StringBuilder("hello");
        // 插入
        sb.insert(sb.length(), "?"); // 尾部插入一个?
        sb.insert(0, '-'); // 首部插入一个-
        System.out.println(sb); // -hello?

        // 移除
        sb.delete(sb.length()-1, sb.length()); // 移除尾部的?
        sb.deleteCharAt(0); // 移除指定索引的字符。移除首部的-
        System.out.println(sb); // hello
Alternatively 2.4 (character)

setCharAt (int index, char ch)
character set for a given index ch.

The character at the specified index as a new character.

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

replace (int start, int end, String str)
given String substring of characters replacement of this sequence of characters.

Starting at the specified position to the end position specified, replaced by other strings.

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

        String s = "world";
        // 用字符串s从索引为6的位置开始替换到索引为11的位置。
        sb.replace(6, 11, s);
        System.out.println(sb); // hello world!
2.6 Reverse string

reverse ()
this character sequence replaced by the reverse form.

StringBuilder string is in, turn it over. This method is quite common.

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

substring (int start, int end) , substring (int start)
Returns a new String, which contains this sequence of sequences contained in the current character.

Substring starts acquiring the character string from the specified location. String and the same.

        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

At last

StringBuilder and StringBuffer class usage is the same, learn any other'll get it.

Several methods are two of them and the String class is the same, basically String learned the analogy.

Related

More commonly used classes see: [Java] commonly used class directory

Published 110 original articles · won 127 Like · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_44034328/article/details/104109136