Java string detailed examples

Source: Internet
Author: User

1. Create an object

For java programsString direct constant, JVM will use a string pool to save them. When a string is directly constant for the first time, JVM puts it into the string pool for caching. In general, string objects in the string pool are not recycled. When the program needs to use this string again, you do not need to create a new string to directly point the referenced variable to an existing string in the string. WhileThe string object created using the new operation does not point to the object in the string pool.But you can use the intern () method to point it to an object in the string pool.Copy codeThe Code is 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
}
}

FAQs
Copy codeThe Code is as follows: String str3 = new String ("abc ");

How many objects have been created?
Answer: Two
Copy codeThe Code is as follows: String str = "AB" + "cd ";

How many objects have been created?

A: One. Both "AB" and "cd" are constants in the string pool. Therefore, only one abcd string pool is created and the string abcd is saved in the string pool.

Copy codeThe Code is as follows: public class StringDemo1 {
Public static void main (String [] args ){
String str1 = "AB ";
String str2 = "cd ";
String str3 = "AB" + "cd"; // create an object and add it to the String pool
String str4 = str1 + str2;
String str5 = str1 + "cd ";
System. out. println (str3 = str4); // false
System. out. println (str3 = str5); // false

}
}

The code above shows that a String object created only when quotes contain text can be added to the String pool, the new object generated by the "+" Join expression containing the new method new object will not be added to the string pool.

However, there is a situation that requires attention:

Copy codeThe Code is 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 an object and add it to the String pool
String str4 = str1 + str2;
String str5 = str1 + "cd ";
System. out. println (str3 = str4); // true
System. out. println (str3 = str5); // true

}
}

Why? The reason is that for constants. Its value is fixed, so it can be determined during compilation.

Change the above Code to see what will happen.

Copy codeThe Code is 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 an object and add it to the String pool
String str4 = str1 + str2;
String str5 = str1 + "cd ";
System. out. println (str3 = str4); // false
System. out. println (str3 = str5); // false

}
}

Although str1 and str2 are defined as constants, they are named immediately. Before calculating the value of s, they are assigned a value and the assigned value is a variable, therefore, they are of the same nature as variables. It can only be created at runtime.

2. String Method

Obtain Method

• Int length ()
• Char charAt (int index) obtains a character based on its location
• Int indexOf (int ch) returns the location where ch first appears in the string.
• Int indexOf (int ch, int fromIndex) obtains the position where ch appears for the first time in the string starting from the position specified by fromIndex.
• Int indexOf (String str)
• Int indexOf (String str, int fromIndex)
• Int lastIndexOf (int ch)

Judgment Method

• Boolean contains (String str) Another judgment method: if (str. index (str )! =-1)
• Boolean startsWith (String str)
• Boolean endsWith (String str)
• Bolean isEmpty (String str)
• Boolean equals (String str)
• Boolean inclusignorecase (String str );

Conversion Method

• Convert character arrays to strings

Constructor

1. String (char [] chs)

2. String (char [] chs, offset, count) converts a part of the 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 to a character array

Char [] toCharArray

• Convert character arrays to strings
• Convert a string to a byte array
Byte [] toBytes

Replacement Method

String replace (olderStr, newStr)

Cutting Method

String split (regex)

Obtain the substring [edit category]

String subString (begin)

String subString (begin, end) contains a header that does not contain the end

Convert string to uppercase/lowercase Android (10)

String toUpperCase ()

String toLowerCase ()

Remove spaces at both ends of the string

String trim ()

Compare two strings in natural order

Int compareTo (String str)

3. String exercise

1. String flip

Copy codeThe Code is 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. obtain the maximum number of identical substrings
Copy codeThe Code is 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 --){
// J indicates the foot mark of the substring.
For (int j = 0; j <len-I + 1; j ++ ){
Str3 = str1.substring (j, j + I );
If (str2.contains (str3 ))
Break outer;

}
}
Return str3;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.