String related class String StringBuffer StringBuilder
All under java.lang package
String
A very special class that can create objects like constants
String str = "abc";
Memory storage "abc" is an object, and the underlying layer is an array, char [] value
The immutable property of String private final char [] value;
When the content is frequently modified, the performance is very poor
StringBuffer
StringBuilder
1. java.lang package
2. There is no inheritance relationship. Object is inherited by default
3. Implement three interfaces -- serializable Appendable CharSequence (character sequence)
4. There is no ComparableTo method in StringBuffer and StringBuilder
StringBuffer and StringBuilder contain a method append() that String does not have; splicing
5. Feature: variable string (variable length and content)
The bottom layer is also an array of char type char [] value, which can be expanded dynamically
The address will not change.
6. It is difficult to build String objects,
Constant method in java, only String is the only way to directly assign a reference type
Construction method without parameters
A construction method with parameters: byte [] char []
7. The difference between StringBuffer/StringBuilder and how to create objects is whether the thread is safe or not
It is not a special reference data type and cannot be assigned directly as a basic data type
Nonparametric construction method
Parametric construction method
//Build an object with a default length of 16 //The bottom layer is an array StringBuilder builder = new StringBuilder(); //Building a custom length space object with given parameters StringBuilder stringBuilder = new StringBuilder(20); //Using the construction method with String parameter, the length is the length of parameter + 16
StringBuilder str = new StringBuilder("abc"); /*StringBuilder builder = new StringBuilder(); //Build an object with a default length of 16 //The bottom layer is an array StringBuilder stringBuilder = new StringBuilder(20); //Building a custom length space object with given parameters StringBuilder str = new StringBuilder("abc"); //Using the construction method with String parameter, the length is the length of parameter + 16*/
Common methods in StringBuilder
The main method:
1. append(); / / there are many overloaded methods. Almost every basic data type can be spliced. Objects can also be spliced (objects can become a toString)
Use this method when frequently splicing strings to improve performance
If we frequently perform add operations, we can use StringBuilder, because it is thread unsafe, and multiple threads can access it at the same time
StringBuilder than StringBuffer Much faster StringBuilder str = new StringBuilder("a"); long time1 = System.currentTimeMillis(); for (int i = 0; i <= 2000000; i++) { str.append("a"); } long time2 = System.currentTimeMillis(); System.out.println(time2 - time1);
2,capacity(): Returns the capacity of the underlying array int 2.1,length();Returns the length of the string int 2.2,setLength();Number of valid elements of modified string 3,charAt(): Returns the character at the specified index location setCharAt(int index,char value);//Change the character in index position to the given value deleteCharAt(int index);//Delete the character in index position
StringBuilder str = new StringBuilder("abcdefg"); str.delete(0,3); str.deleteCharAt(1); str.setCharAt(0,'c'); System.out.println(str);
4, codePointAt(): returns the code corresponding to the character in the specified index position String = substring(int start,int end); You need to receive the return value to see the new string effect StringBuilder = delete(int start,int end); The unique method String class in StringBuilder class does not Delete the string between start and end, and wait for the effect without receiving the return value StringBuilder = deleteCharAt(int index); Method not in String class, delete the element in an index position 5,ensureCapacity(int minimumCapacity) Make sure that the capacity of the underlying array is sufficient. Call this method to make sure that the capacity of the array is sufficient 6,indexOf(String str int fromIndex); 7,lastIndexOf(String str,int fromIndex); Find the index position of the given str in the string for the first time, with overload 8,
insert(int offset,String str) //Insert a string in the specified index insert(int offset, boolean b) //Inserts a boolean type into the specified index insert(int offset, char c) insert(int offset, char[] str) insert(int index, char[] str, int offset, int len) insert(int dstOffset, CharSequence s) insert(int dstOffset, CharSequence s, int start, int end) insert(int offset, double d) insert(int offset, float f) insert(int offset, int i) insert(int offset, long l) insert(int offset, Object obj) insert(int offset, String str) insert()Inserts the given element into the specified index location
9,
replace(int start ,int end ,String str)//Replace the start to end element with str StringBuilder str = new StringBuilder("abcdefg"); str.replace(0,3,"dpw"); System.out.println(str);
10,
reverse();Invert elements StringBuilder str = new StringBuilder("abcdefg"); str.reverse(); System.out.println(str);
11,
toString(); build StringBuilder object into String object to return StringBuilder str = new StringBuilder("abc"); System.out.println(str.toString()); The result is: abc
12, trimToSize(); remove the useless capacity from the array and turn it into an array of length length