1. The meaning of Sting class
Sring class: represents a string. Is a final class that represents an immutable sequence of characters. Strings are constants and are represented by double quotes. Values cannot be changed after creation. The String contents of a String object are stored in a character array value[].
2. Some features of the String class
String: A String of characters, represented by a pair of”
-
String is declared final and cannot be inherited
-
String implements the Serializable interface: String supports serialization.
String implements the Comparable interface: Strings can be compared
-
Sring internally defines final char[] value to store string data
-
String: represents an immutable sequence of characters. Immutability for short.
1. When reassigning a value to a string, you need to rewrite the assignment of the specified memory area instead of using the original value. 2. When you connect an existing string, you also need to assign a new value to the memory area instead of using the original value. 3. When you call String's replace () method to change the specified character or String, you also need to reassign the memory area. The original value cannot be used for assignment.Copy the code
-
Assigns a value to a string literal (as opposed to new), where the string value is declared in the string constant pool.
-
A string constant pool is a string that does not store the same content.
3. How to instantiate the String object
The way literals are defined
String STR = "hello";Copy the code
Through the constructor
String s1=new String();
String ss2=new String(String original);
String s3=new String(char[] a);
String s4=new String(char[] a,int startIndex,int count);
Copy the code
Literals define data in the string constant pool in the method area, that is, identical strings share an address value.
The data declared by the new constructor is stored in heap space.
The class instantiates the value passed in as String, as shown in the figure. The argument constructor uses the same literal assignment as the name address of two strings
String s=new String (" ABC "); How many objects are created in memory? Char [] = 'ABC'; char[] = 'ABC';Copy the code
Pay attention to the point
Sring s1 = “hello”; String s2 = “he”; String s3 = s2 + “llo”;
At this point, S3 opens up addresses in the heap. String s4=s3. Intern (); // The method forces S3 to declare the same address as S1 in the constant pool. Final declared string types open space in the constant pool regardless of how they are assignedCopy the code
4. Common methods
// String s2= s1.tolowerCase (locale.root); System.out.println(s2); System.out.println(s1); / / upper s2 = s1. ToUpperCase (Locale. ROOT); System.out.println(s2); System.out.println(s1); String s3=" hello world "; String s4=s3.trim(); System.out.println("-----"+s3+"-----"); System.out.println("-----"+s4+"-----"); System.out.println(s2.equalsignorecase (s1)); / / connection System. Out. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "); s3="abc"; s4=s3.concat("def").concat(" ").concat("aaa"); System.out.println(s4); // Compare String size // less than 0, currently small String s5=" ABC "; String s6="abe"; System.out.println(s5.compareTo(s6)); S7 ="abcdef"; s7="abcdef"; System.out.println(s7.substring(2)); System. The out. Println (s7. Substring (2, 5)); S1 =" adASdaddsd "; s1=" adASdaddsd "; s1=" adASdaddsd "; s1=" adASdaddsd "; boolean b1=s1.endsWith("dsd"); System.out.println(b1); System.out.println(s1.startsWith("a")); System.out.println(s1.startsWith("as",2)); Println (s1. Contains ("sdad")); system.out.println (s1. Contains ("sdad")); System.out.println(s1.indexof ("dad"))); Println (s1.indexof ("dad",3)); system.out.println (s1.indexof ("dad",3)); System.out.println(s1.lastIndexof ("sd")); S1 =" helloWorld,world"; s1=" helloWorld "; s1=" helloWorld "; s1=" helloWorld "; s1=" helloWorld "; s1=" helloWorld "; s1=" helloWorld "; s2=s1.replace("l","r"); // Replace the character system.out.println (s1); System.out.println(s2); s2=s1.replace("world","java"); // Replace the string system.out.println (s2); s2=s1.replaceFirst("world","java"); System.out.println(s2); // Replace the number in the string with "," s1="1hello2world3"; s2 = s1.replaceAll("\\d+", ","); System.out.println(s2); / / delete s2 = s1. The beginning and end of a comma replaceAll (" \ \ d + ", ", "). The replaceAll (" ^, |, $", ""); System.out.println(s2); // Regular expression // match // Match the given regular expression s1="12345"; System.out.println(s1.matches("\\d+")); S2 ="0571-4534289"; System. The out. Println (s2. Matches (" 0571 - \ \ d {7, 8} ")); / / section / / to cut the specified character string into an array of strings s1 = "hello | world | Java". String[] s1s=s1.split("\\|"); System.out.println(s1); for (String s:s1s){ System.out.print(s+" "); } System.out.println(); s2="hello.world.java"; System.out.println(s2); String[] s2s=s2.split("\\."); for (String s:s2s){ System.out.print(s+" "); }Copy the code
5.String converts to char and byte arrays
Char [] : call toCharArray ()
Conversion to byte[] : call method getBytes ()