In Java, the string is an object, is the basic data structure describing the character, and is also a kind of data structure which is very important in the program design.
Java provides two classes specifically for storing and handling strings: String & StringBuffer
There are three common ways to create a String type object:
1. by initializing String str = "Hellow";
2. Create a string str = new string () using the new keyword;
3. Create String str = double.tostring (123.45) by type conversion;
Summary of common methods of the string class:
1.String method of seeking length: public int length ();
1 String s1 = double.tostring (1234.5678); 2 System.out.println ("The length of S1" + S1 + "is:" +s1.length ());
2. Interception method: Public String substring (int begin); Returns the string from begin to last
Public String substring (int begin, int end);
1 String sub1 = s1.substring (5); 2 String sub2 = s1.substring (2, 7); 3 System.out.println (sub1); 4 System.out.println (SUB2);
3. Go to string array method: Public string[] Split (string regex);
1 /** 2 * Common way to divide strings by spaces 3 */ 4 String s2 = "My name is gap, I study in ustc!" ; 5 String snum[] = S2.split ("{1,}"); 6 for (int i=0; i<snum.length; i++) {7 System.out.println (Snum[i]); 8 }
4. Method for determining substring position: public int indexOf (string str);
public int indexOf (String str, int fromIndex);
1 int n1 = s2.indexof ("ga"); 2 System.out.println (s2.indexof (' I ')); // the output is 8, which is the I 3 System.out.println (s2.indexof (' I ',)); // The output is 16 i.e., I in I
Then attach a very interesting face question, but also can deepen the understanding of Java memory allocation:
1 Public classTest {2 Public Static voidMain (string[] args) {3 //The following 4 lines of programs create several objects4String str1 =NewString ("Hello");5String str2 = "Hello";6 7String STR3 =NewString ("Hello");8String STR4 = "Hello";9 Ten System.out.println (Str1.equals (STR3)); OneSystem.out.println (str1 = =str2); ASystem.out.println (str1 = =STR3); -System.out.println (str2 = =STR3); -System.out.println (str2 = =STR4); the } -}
The 4th to 8th line of statements creates several objects at the time of execution. 10~14 statement output results Why?
The four-line statement executes with two objects created, and the fourth statement executes in order of 1. Allocate space for str1 on the Stack 2. Create the object "Hello" in the string pool, or memory data segment. Create a "Hello" object in the heap 4. Assigning references to Str
The five-line statement does not create the object when it is executed because it first checks if there is a "hello" object in the string pool, and str2 directly points to "Hello" in the string pool;
The seventh line of execution will create an object that creates a "Hello" object on the heap;
Similarly, the eighth line of the statement does not create the object, it is worth noting that because there is only one "Hello" object in the string pool, so str2 and STR4 actually point to the same piece of memory area, so str2 = = STR4;
Operation Result:
The String class in Java