1. Creating objects
For string Direct Constants in Java programs, the JVM uses a string pool to hold them. When a direct constant of a string is used for the first time, the JVM puts it in a string pool for caching. In general, string objects in a string pool are not garbage collected. When the program needs to use the string again, you can simply let the reference variable directly point to the string already in the string without recreating a new string. the string object created using the new operation does not point to an object in the string pool , but you can use the Intern () method to point it to an object in the string pool.
Copy Code code as follows:
public class StringDemo1 {
public static void Main (string[] args) {
String str1 = "abc";
String str2 = "abc";
String Str3 =new string ("abc");
System.out.println (STR1==STR2);//true
System.out.println (STR1==STR3);//false
}
}
FAQ
Copy Code code as follows:
String Str3 =new string ("abc");
How many objects are created?
Answer: Two
Copy Code code as follows:
String str = "AB" + "CD";
How many objects are created?
Answer: one. Both "AB" and "CD" are constants placed in a string pool. Therefore, only one ABCD string pool is created and the string ABCD is saved in the string pool.
Copy Code code as follows:
public class StringDemo1 {
public static void Main (string[] args) {
String str1 = "AB";
String str2 = "CD";
String STR3 = "AB" + "CD";//Create object and join string pool
String STR4 =str1+str2;
String STR5 =str1+ "CD";
System.out.println (STR3==STR4);//false
System.out.println (STR3==STR5);//false
}
}
It is known from the above code that a string object created only if the quotation marks contain text can be added to the string pool, and that the new object created by the "+" join expression that contains the new method is not added to the string pool.
But there is one situation that needs to be brought to our attention:
Copy Code code as follows:
public class StringDemo1 {
Private final static String str1 = "AB";
Private final static String str2 = "CD";
public static void Main (string[] args) {
String STR3 = "AB" + "CD";//Create object and join string pool
String STR4 =str1+str2;
String STR5 =str1+ "CD";
System.out.println (STR3==STR4);//true
System.out.println (STR3==STR5);//true
}
}
Why is that? The reason is this, for constants. Its value is fixed, so it can be determined during compilation.
Slightly change the code above to see what will happen.
Copy code code as follows:
public class StringDemo1 {
Private final static String str1;
Private final static String str2;
static{
str1= "AB";
str2= "CD";
}
public static void Main (string[] args) {
String STR3 = "AB" + "CD";//Create object and join string pool
String STR4 =str1+str2;
String STR5 =str1+ "CD";
System.out.println (STR3==STR4);//false
System.out.println (STR3==STR5);//false
}
}
STR1 and str2 are defined as constants, but their reputation is immediately assigned, and they are assigned a value before the value of S is calculated, and what values are assigned are variables, so the properties and variables are the same. Can only be created at run time.
2. String method
Get method
int Length ()
char charAt (int index) gets a character from the position
int indexOf (int ch) returns the position in which CH first appears in the string
int indexOf (int ch,int fromindex) starts at the specified location in the Fromindex and gets the first occurrence of CH in the string
int indexOf (String str)
int indexOf (String str,int fromindex)
int lastindexof (int ch)
Judging method
Boolean contains (String str) Another way of judging: if (Str.index (str)!=-1)
boolean startswith (String str)
boolean endsWith (String str)
Bolean IsEmpty (String str)
boolean equals (String str)
boolean equalsignorecase (String str);
Conversion methods
• Convert a character array to a string
Constructors
1.String (char[] CHS)
2.String (char[] chs,offset,count) Converts a portion of a character array into a string.
static method
1.static String copyvalueof (char[] CHS)
2.static String copyvalueof (char[] chs,int offset,int count)
3.static String valueof (char[])
4.static String valueof (char[] chs,int offset,int count)
• Convert a string into a character array
Char[] ToCharArray
• Convert character array to string
• Convert a string into a byte array
Byte[] Tobytes
Replace method
String Replace (OLDERSTR,NEWSTR)
Cutting method
String Split (Regex)
Get substring [Edit category]
String subString (BEGIN)
String subString (begin,end) contains headers that do not contain tails
Converts strings to uppercase and lowercase Android (10)
String toUpperCase ()
String toLowerCase ()
Remove the spaces at both ends of the string
String Trim ()
A natural order comparison of two strings
int CompareTo (String str)
3.String Practice
1. String Flip
Copy Code code as follows:
public class StringDemo2 {
public static void Main (string[] args) {
String str = "Avdkfasjks";
Reversemethod_1 (str);
}
public static void Reversemethod_1 (String str) {
for (int i=str.length (); i>0;i--) {
System.out.print (Str.charat (i-1));
}
}
}
2. Get maximum identical substring
Copy Code code as follows:
public class StringDemo2 {
public static void Main (string[] args) {
String str1 = "Avdkfasjks";
String str2 = "Ewavdrtte";
System.out.println (Commonmaxsubstring (str1, str2));
}
public static string Commonmaxsubstring (String str1,string str2) {
int len = Str1.length ();
String STR3 = null;
Outer
I is the length of the substring
for (int i = len;i>0;i--) {
Pin of J as substring
for (int j=0;j<len-i+1;j++) {
Str3=str1.substring (J,j+i);
if (Str2.contains (STR3))
Break outer;
}
}
return STR3;
}
}