Java study notes-stringbuilder and stringbuffer

I encountered this problem in the interview, but the result got stuck, but the foundation is still not solid, so please review it at any time under the mark.

Reprinted article: https://blog.csdn.net/u011702479/article/details/82262823

 

Simply summarize the two but don't:

1. Stringbuilder is generally used, faster, but the thread is not safe

2. Stringbuffer is rarely used, thread-safe, and slow

 

Commonly used methods

The following is just a part, the rest can be studied by yourself, after reading it, they are all relatively simple

public class Test {

    public static void main(String[] args){

        StringBuilder stringBuilder=new StringBuilder("testStringBuilder");
        //拼接
        stringBuilder.append(1);
        System.out.println(stringBuilder);

        //根据字符串获取对应的下标
        System.out.println(stringBuilder.indexOf("t"));

        //反转
        stringBuilder.reverse();
        System.out.println(stringBuilder.toString());

        //获取字符串长度
        System.out.println(stringBuilder.length());
        
    }

}

 

Guess you like

Origin blog.csdn.net/mumuwang1234/article/details/113846555