String common operations:
1. Use the charAt method of the String class to retrieve a certain byte. The count starts from 0:
String a = "Hello"; // a. charAt (1) = 'E'
2. Use the substring method to obtain the substring. For example
System. out. println (a. substring (0, 4) // output "Hell"
Note: The characters at the position of the substring's 2nd parameters are not included. In this way, s. substring (a, B) always contains B-a characters.
3. replaceAll) to replace the corresponding string
Public class Main {public static void main (String [] args) {String str = "dasdsad3213adasd3123"; String strTemp = str. replaceAll ("[a-zA-Z]", ""); System. out. print (strTemp); // output 32133123 }}
ReplaceAll is used to replace all English characters in the string with null characters.
4. Use the split () method to split strings
Public class Main {public static void main (String [] args) {String str = "2013/09/24"; String [] strTemp = str. split ("/"); for (int I = 0; I <strTemp. length; I ++) {System. out. print (strTemp [I] + ""); // output 2013 09 24 }}}
5. You can use the + sign for string connection, as shown in figure
String a = "Hello ";
String B = "world ";
System. out. println (a + "," + B + "! "); // Output" Hello, world! "
To directly change a byte in a string, you can use another StringBuffer class.
Difference between String and StringBuffer
String |
StringBuffer |
A) immutable object. Modifying existing String objects is to re-create a new object.
B) assign values directly or assign values through constructors.
Difference: assign values directly. First, judge whether the same string exists in the java string pool, so you do not need to redefine the sharing design ). If the constructor assigns a value, a new unit needs to be created in the memory each time.
C) String overwrites the equals method and hashCode.
Method. Suitable for set operations. |
A) mutable object
B) assign values only through Constructors
C) StringBuffer does not overwrite the equals and hashCode methods.
In general, StringBuffer is more efficient than String connection operations. Append) method. |
Key points: 1. It is considered that append () is more efficient than "+.
This is because "+" is the most efficient when compiling strings to determine the value. Example: String result = "hello" + "world"; // an object 2. Do not use new) to create a String. Because new () is used to open up new memory. 3. Avoid using "+ =" to construct a string. 4. When declaring the StringBuffer object, set the appropriate capacity. Do not use the default value 16) 5. Pay attention to the use of intern. S. intern () = t. intern () is true, if and only if s. Except st) is true. That is, the content is the same. |
Conversion between string, int, and char []
String to char |
Char ch [] = str. toCharArray () |
Char to String |
1) String s = new String (ch );
2) String s = ch. toString (); |
String to int (this String is numeric or an error occurs during conversion) |
1) Int I = Integer. parseInt (str)
2) Int I = Integer. valueOfstr). intValue; |
Convert int to String
|
1) String s = String. valueof (I );
2) String s = Integer. toString (I );
3) String s = "" + I; |
This article is from the blog of "Italian suckling pig", please be sure to keep this source http://lovesisi.blog.51cto.com/6414433/1301164