1, String class
1. = = difference from equals
==The comparison is the reference type, whether the address values are the same and whether they are the same object
Equals compares reference types. By default, it compares address values. The String class overrides the equals method to compare whether the contents are the same
public class App { public static void main(String[] args) throws Exception { String str1 = "abc"; String str3 = new String("abc"); System.out.println(str1 == str3); System.out.println(str1.equals(str3)); } }
Because str1 and str3 are not the same object, str1==str2 is false
But they both have the same content, so STR1 Equals (STR2) is true
2. Define a string
String str1 = "string1"; String str2 = new String("string2");
Differences between the above two definitions:
str1 will first find out whether there is a constant whose content is "string1" in the constant pool. If so, str1 will point to this constant; If not, a new object with string1 as the content will be created in the constant pool.
str2 will directly create an object first (if it already exists in the constant pool, it only needs to be created in the heap) or two objects (there is no constant pool, so it needs to be created in the heap and constant pool).
Case analysis
public class App { public static void main(String[] args) throws Exception { String str1 = "abc"; String str2 = "abc"; String str3 = new String("abc"); String str4 = new String("abc"); System.out.println(str1 == str2); System.out.println(str3 == str4); System.out.println(str1 == str3); System.out.println(str1.equals(str3)); } }
When creating str1 and str2, you will first check whether there is a constant of "abc" in the constant pool, and then point str1 and str2 to the constant. Therefore, str1 and str2 point to the same object, so str1==str2 is true.
Str3 and str4 create objects respectively. They point to different objects, so str3==str4 is false.
Similarly, str1 and str3 cannot be the same object, so str1==str3
It's false.
Then these four contents are "abc", so no matter which one uses the equals method, it is true.
3.String seems to be able to modify its value
This idea is wrong
public class App { public static void main(String[] args) throws Exception { String str1 = "abc"; str1 = "change"; System.out.println(str1); } }
It looks like str1 has been reassigned. In fact, str1 points to the "abc" object in the constant pool instead of the "change" object in the constant pool. Instead of str1, the original value of the object was "abc" and was modified to "change".
4. String addition
public class App { public static void main(String[] args) { String str1 = "abc" + "d"; String str2 = "abc"; String str3 = "d"; String str4 = str2 + str3; System.out.println(str1 == str4); } }
When str1 is compiled, the computer will directly connect "abc" and "d" to obtain "abcd", and then check whether there is "abcd" in the constant pool; When str4 is compiled, it does not know what is in str2 and str3, so it will directly create an object in the constant pool. So str1 and str4 do not point to the same object.
5. Method of string class - search function
5.1 find the index of a substring in the string
Find the first index indexOf("character / string") from front to back
Starting from a sequence, the index indexof the first occurrence of the substring ("character / string", (int) starts from here)
public class App { public static void main(String[] args) { String str1 = "abcdc abc"; System.out.println(str1.indexOf("c")); System.out.println(str1.indexOf("bc")); System.out.println(str1.indexOf("c",5)); System.out.println(str1.indexOf("bc", 5)); } }
5.2 find the index of the first occurrence of a substring in the string from back to front
lastIndexOf("character / string")
lastIndexOf("character / string", (int) start here and look back)
public class App { public static void main(String[] args) { String str1 = "abcdc abc"; System.out.println(str1.lastIndexOf("c")); System.out.println(str1.lastIndexOf("bc")); System.out.println(str1.lastIndexOf("c",5)); System.out.println(str1.lastIndexOf("bc", 5)); } }
5.3 return the character at the index position in the string
charAt((int) index value)
public class App { public static void main(String[] args) { String str1 = "abcdc abc"; System.out.println(str1.charAt(7)); //B (last b) } }
5.4 judge whether the string starts with a substring
startsWIth("character / string")
==startsWIth("character / string", n) = = start at index n and check whether the string starts with a substring.
public class App { public static void main(String[] args) { String str1 = "abcdc abc"; System.out.println(str1.startsWith("a")); System.out.println(str1.startsWith("a",6)); System.out.println(str1.startsWith("a",5)); } }
5.5 judge whether the string ends with a substring
endsWIth("character / string")
public class App { public static void main(String[] args) { String str1 = "abcdc abc"; System.out.println(str1.endsWith("c")); //true } }
5.6 judge whether the string contains a substring
contains("character / string")
public class App { public static void main(String[] args) { String str1 = "abcdc abc"; System.out.println(str1.contains("c")); //true } }
6. Method of string class - comparison function
6.1 judge whether the string content is empty
str.isEmpty();
6.2 judge whether the contents of strings are equal
String str1 = "Abc"; String str2 = "abc"; String str3 = "Abc"; str1.equals(str2); //false str1.equals(str3); //true
6.3 judge whether the contents of strings are equal regardless of case
String str1 = "Abc"; String str2 = "abc"; str1.equals(str2); //true
6.4 ascending comparison results of strings
str1.compareTo(str2)
str1 == str2, output 0
STR1 < STR2, negative number output
STR1 > STR2, output integer
In fact, it is the result of str1-str2 to design the calculation of ASCII code
public class App { public static void main(String[] args) { String str1 ="a"; String str2 = "c"; System.out.println(str1.compareTo(str2)); //-2 System.out.println(str2.compareTo(str1)); //2 } }
7. Create and return a new String object
7.1. Connect two strings
str1.concat(str2);
7.2. Replace a part of the string and replace all the same parts
public class App { public static void main(String[] args) { String str1 ="I'm bad, you're bad, and so is he"; System.out.println(str1.replace("decline","Handsome")); } }
7.3. String interception
Intercept the string of index [a,b)
public class App { public static void main(String[] args) { String str1 ="aabcd"; System.out.println(str1.substring(0,3));//aab } }
7.4. Ignore the space before and after the string, and the space in the middle is not ignored
public class App { public static void main(String[] args) { String str1 =" hello java "; System.out.println(str1.trim());//hello java } }
7.5 case conversion
public class App { public static void main(String[] args) { String str1 ="hello JAVA"; System.out.println(str1.toUpperCase());//HELLO JAVA System.out.println(str1.toLowerCase());//hello java } }
7.6. Divide the string according to certain rules and return a string array
Escape characters such as., $, |, and * must be added\
Multiple separators, you can use | as a hyphen
public class App { public static void main(String[] args) { String str1 ="hello-java-I-love-java"; for ( String str: str1.split("-")) { System.out.println(str); } } }
7.7. Convert string to character array (char)
public class App { public static void main(String[] args) { String str1 ="hello-java-I-love-java"; System.out.println(str1.toCharArray());//hello-java-I-love-java } }
7.8. Convert string to byte array
public class App { public static void main(String[] args) { String str1 ="hello-java-I-love-java"; System.out.println(str1.getBytes()); } }
2, StringBuilder class and StringBuffer
StringBuilder: variable character sequence, unsafe thread, high efficiency
StringBuffer variable character sequence, thread safety, thread synchronization check, low efficiency
The methods of StringBuilder and StringBuffer are basically the same
1. A method similar to String
str1.length(); str1.indexOf("n"); str1.lastIndexOf("n"); str1.charAt(i); str1.substring(a,b);
2. Common methods
str1.append(xxx);
Appending to an object does not generate a new object
3. Methods to know
//Insert character at index position StringBuffer str1 = new StringBuffer("Honey Snow Ice City"); str1.insert(0,"love"); //Aimi Snow Ice City //Delete string [a,b) str1.delete(0.2); //Snow and Ice City //Deletes the character at the specified position in the string StringBuffer str2 = new StringBuffer("i don't love you"); str2.deleteCharAt(1); //I love you! //Reverse string order str2.reverse(); //You love me