String stands for strings, and in Java all string literals are implemented as instances of this class
Character of the string and simple analysis of the principle:
Packagedemo;/** Features of the String class: * All "" is a String object * Once created is a constant, cannot be changed*/ Public classStringdemo { Public Static voidMain (string[] args) {//Discovery created object does not require newString str = "ABCD"; System.out.println (str); //output: ABCD instead of memory address because the string class overrides the ToString methodstr = "EFGH"; System.out.println (str); //output: Efgh, didn't it say that constants don't change? Here's why://The string itself is an object, in heap memory, where the string essence is an array of characters//Source: Private final char value[]; there is a final modification, so string is a constant//str = "EFGH" is reopened in memory, the direction of STR has changed, and the "ABCD" string object has not changed }}
How the String class is created and compared:
Packagedemo; Public classStringdemo { Public Static voidMain (string[] args) {//The string is defined in two ways, direct =, using the construction method of stringString str1 =NewString ("ABC"); String str2= "ABC"; //Direct = more convenient, but there are differencesSystem.out.println (str1==STR2);//falseSystem.out.println (Str1.equals (str2));//true//use the construction method to define the principle://two objects are actually created, one is a new string object, and the other is an "ABC" Object//The new string object holds the address of the character array object "ABC".//str1 pointing to the new string object//when printing str1, call the overridden ToString method to find the string//because the "ABC" Object already exists//so str2 directly points to the "ABC" object and does not create a new object//Therefore, STR1==STR2 is false because the address of the object is different//while Str1.equals (STR2) is true, the principle://The string class inherits object, overrides the parent class method equals, and establishes the string's own string comparison method//that is, as long as each character of the string is true, regardless of the address }}
How to construct the String class:
1. Official information: string (byte[] byte) constructs a new string by decoding the specified byte array using the platform's default character set
Example:
Packagedemo;/** Construction method of the string class*/ Public classStringDemo2 { Public Static voidMain (string[] args) {byte[] bytes = {97,98,99,100}; String s=NewString (bytes); System.out.println (s); //output: ABCD//The result of querying the encoded table for each byte in the byte array }}
2. Official information: String (byte[] byte, int offset, int length)
Example:
Packagedemo;/** Construction method of the string class*/ Public classStringDemo2 { Public Static voidMain (string[] args) {byte[] bytes = {65,66,67,68,69}; String s=NewString (Bytes, 2, 3); //The second argument is the starting index, and the third parameter is the number of selectionsSystem.out.println (s); //Output: CDE }}
3. Official information: string (char[] value) assigns a new string that represents the sequence of characters currently contained in the character array parameter
Example:
Packagedemo;/** Construction method of the string class*/ Public classStringDemo2 { Public Static voidMain (string[] args) {function (); } Public Static voidfunction () {Char[] ch = {' A ', ' B ', ' C ', ' d ', ' e '}; String s=NewString (CH); System.out.println (s); }}//Output: ABCDE
4. Similar to the first two, official information: String (char[] value, int offset, int length)
Example:
Packagedemo;/** Construction method of the string class*/ Public classStringDemo2 { Public Static voidMain (string[] args) {function (); } Public Static voidfunction () {Char[] ch = {' A ', ' B ', ' C ', ' d ', ' e '}; String s=NewString (ch,1,3); System.out.println (s); }}//Output: BCD
There are a lot of construction methods, but these are often
Other methods of the string class:
1. Official Information: int length () returns the length of this string (relatively simple, no example)
2. Official information: string substring (int beginindex) returns a new string that is a substring of this string
Another: String substring (int beginindex,int endIndex) Ibid., one more end index value
Example:
Packagedemo; Public classStringDemo2 { Public Static voidMain (string[] args) {function (); } Public Static voidfunction () {String str= "Iamhandsome"; String str1= Str.substring (3); String str2= Str.substring (3, 5); System.out.println (STR1); System.out.println (STR2); }}/*output: Handsomeha Note here that the interval is left closed to the right, meaning to include the starting index, not including the terminating index*/
3. Official Information: Boolean startsWith (String prefix)
Tests whether a string starts with the specified prefix
There should also be suffixes with prefixes:
Boolean endwith (String suffix)
4. Official Information: Boolean contains (string s) to determine if a string contains another string
5.int indexOf (char ch) finds the first occurrence of a character in a string index
3,4,5 Example:
Packagedemo; Public classStringDemo2 { Public Static voidMain (string[] args) {function (); } Public Static voidfunction () {string string= "Iamhandsome"; Booleanb = String.startswith ("Ia"); System.out.println (b);//true BooleanB1 = String.endswith ("ome"); System.out.println (B1);//true BooleanB2 = String.contains ("Hand"); System.out.println (B2);//true intA = String.IndexOf ("a"); System.out.println (a);//1 }}
6. Turn the string into a byte array:
Byte[] GetBytes ()
This feature is constructed in the opposite way as String
Example:
Packagedemo; Public classStringDemo2 { Public Static voidMain (string[] args) {function (); } Public Static voidfunction () {String str= "ABC"; byte[] bytes =str.getbytes (); for(inti=0; i<bytes.length; i++) {System.out.println (bytes[i]); } }}/*Output: 979899*/
7. Convert the string to a character array: char[] ToCharArray ();
Packagedemo; Public classStringDemo2 { Public Static voidMain (string[] args) {function (); } Public Static voidfunction () {String str= "ABC"; Char[] chars =Str.tochararray (); for(inti=0; i<chars.length; i++) {System.out.println (chars[i]); } }}/*output: ABC*/
Compare strings if they are identical
Example:
Packagedemo; Public classStringDemo2 { Public Static voidMain (string[] args) {function (); } Public Static voidfunction () {String str1= "ABC"; String str2= "ABC"; Booleanb1=str1.equals (STR2); BooleanB2 =str1.equalsignorecase (STR2); System.out.println (B1);//falseSystem.out.println (B2);//true//compare strings are the same, the second ignores case }}
Java Learning Note (String Class)