Regain the basics of java (13): Summary of String sisters StringBuffer and StringBuilder
1. Overview of the StringBuffer class
buffer: Buffer
2. String buffer, very similar to String, is used to store string data
3. Both the String class and the StringBuffer class have a char array, which is the buffer. We cannot operate the buffer of the String class, but the buffer of the StringBuffer class can control its length.
4 .java.lang package
5. When the buffer is not enough, it can automatically grow
2. Construction method
public StringBuffer(),无参构造方法,构造一个空的字符串缓冲区,初始容量为16个字符 public StringBuffer(int capacity),构造一个指定容量的空的字符串缓冲区 public StringBuffer(String str),使用一个字符串作为初始内容来构造一个字符串缓冲区,并在后面预留16个字符的空缓冲区 1. StringBuffer sb="hello"; 不行 2. StringBuffer sb=new StringBuffer("hello"); sb+"world"; 不行
3. Functional method
public StringBuffer append(任意类型 o),把任意类型数据的字符串表达形式追加到缓冲区的最后(例如:如果是对象,追加的是其toString方法的返回值) public StringBuffer insert(int offset,任意类型 o),把任意类型的字符串表达形式插入到缓冲区指定位置 public int capacity(),获得字符串缓冲区的当前容量 public int length(),获得字符串缓冲区内字符串的长度 public StringBuffer delete(int start,int end),删除缓冲区指定起始位置的字符串 public StringBuffer deleteCharAt(int index),删除缓冲指定位置的字符 public StringBuffer replace(int start,int end,String str),把缓冲区指定位置的字符串替换为新的字符串 public StringBuffer reverse(),字符串反转(倒) public String toString(),把StringBuffer转换为String类型
4. The difference between String and StringBuffer
1. StringBuffer sb="hello"; 不行 2. StringBuffer sb=new StringBuffer("hello"); sb+"world"; 不行 3. String对象是不可变的,StringBuffer对象是可变的(画内存分配图) 4. StringBuffer保证线程安全(数据同步),String不保证线程安全(数据不同步)
5. StringBuilder class
It is the same as StringBuffer, the two classes are compatible
2. This class does not guarantee thread safety
3. Without considering multi-threading, characters The efficiency of the three string sisters: StringBuilder class> StringBuffer class> String class If you connect string constants, it is more efficient to use the "+" of the string; if you connect string variables, it is more efficient to use the append method of StringBuffer
6. Packaging class
7. The main functions of packaging classes
These classes provide a few Functional method, you can convert String type data into a wrapper class or basic data type 1. Convert between String and wrapper class a. Convert wrapper class to String Integer i=100; i+"" toString() b.String conversion For the wrapper class valueOf: Which class this method is in, then its role is to convert other types into the class it is in 2. Mutual conversion between String and basic data types a. Convert basic data types to String + "" b .String converted to basic data type8. Regular expression
9. How to write regular expressions
1. First write the fixed 2. Then write it in blocks. Each block first specifies the type and then the number10. How to use regular expressions for data verification
1.String类的public boolean matches(String regex){}String email="601141632@qq.com";String email_regex=".+@//w+//.[a-z]{2,}";email.matches(email_regex); 2.java.util.regex包中有个Pattern类public static matches(String regex,String input){}
11.