Home > Java > javaTutorial > Regaining the basics of Java (13): Summary of String sisters StringBuffer and StringBuilder

Regaining the basics of Java (13): Summary of String sisters StringBuffer and StringBuilder

黄舟
Release: 2017-01-16 10:08:21
Original
1130 people have browsed it

Regain the basics of java (13): Summary of String sisters StringBuffer and StringBuilder

1. Overview of the StringBuffer class

  1. 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";   不行
Copy after login

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类型
Copy after login

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不保证线程安全(数据不同步)
Copy after login

5. StringBuilder class

  1. 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

  1. ##Java has 8 basic data types: byte, short, int, long;float, double;char, boolean

2. String s="100"; String s="99.999" ;

3. Java provides reference data types corresponding to basic data types: Byte, Character, Short, Integer, Long, Float, Double, Boolean

4. Whether they are reference data types or basic data types, their functions are the same. The main difference is: reference types can provide methods, but basic data types cannot.

5. The reference data type corresponding to the basic data type is called a wrapper class

6. Usage of wrapper class Integer i=new Integer(100); //Boxing //int i=100; Integer j=100; //Automatic boxing (after JDK5) System.out.println(i.intValue()+100); //Unboxing System .out.println(i+100); //Automatic unboxing (after JDK5)

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 type


8. Regular expression

  1. The program needs to verify the data entered by the customer

2. The role of regular expressions is to verify data (for format)

3. Example: Zhengzhou fixed phone number (0371-56061160-223) Area code Fixed to 0371. The phone number is fixed to 8 digits and cannot start with 0. The extension number is optional, 1-3 digits are all numbers. Use "-" to separate

4. String zz_phone="0371-[1- 9]//d{7}(-//d{1,3})?";

5. Regular expressions exist in the form of strings

6. Regular expressions An expression is composed of a bunch of special symbols, used to describe or express the format of a certain data

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 number


10. 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){}
Copy after login

11.

  1. The split method of String class supports regular expressions

2 The replace method of the .String class does not support regular expressions, but replaceAll supports regular expressions


The above is to regain the basics of java (13): String The content summarized by sisters StringBuffer and StringBuilder. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template