Java learns thirty (String and StringBuffer classes) from scratch, and java clears stringbuffer
I. StringBuffer connection character operations when the content of a string needs to be changed frequently, use StringBuffer TO USE THE append () method in StringBuffer to complete string connection operations II. Common StringBuffer Methods
No. |
Method Definition |
Type |
Description |
1 |
Public StringBuffer () |
Structure |
StringBuffer Constructor |
2 |
Public StringBuffer append (char c) |
Method |
A large number of append operations are provided in StringBuffer (similar to "+" in String). content can be appended to StringBuffer. This method can be used to add any data types. |
3 |
Public StringBuffer append (String str) |
Method |
4 |
Public StringBuffer append (StringBuffer sb) |
Method |
5 |
Public int indexOf (String str) |
Method |
Queries whether a specified string exists. |
6 |
Public int indexOf (String str, int fromIndex) |
Method |
Query whether a specified string exists from the specified position |
7 |
Public StringBuffer insert (int offset, String str) |
Method |
Add the specified string at the specified position |
8 |
Public StringBuffer reverse () |
Method |
Reverse save content |
9 |
Public StringBuffer replace (int start, int end, String str) |
Method |
Content replacement |
10 |
Public int length () |
Method |
Obtain the Content Length |
11 |
Public StringBuffer delete (int start, int end) |
Method |
Deletes a string from a specified range. |
12 |
Public String substring (int start) |
Method |
String truncation, specifying the start point |
13 |
Public String substring (int start, int end) |
Method |
Truncates a string from a specified range. |
14 |
Public String toString () |
Method |
Object class inheritance method, used to change the content to the String type |
Iii. Use the String class
Package com. pb. demo1; public class StringTest {public static void main (String [] args) {String str = "Learn Java! "; // Length () method System. out. the length of println (str + "is:" + str. length (); // converts the case sensitivity toLowerCase (), toUpperCase () System. out. println ("to lowercase:" + str. toLowerCase (); System. out. println ("converted to uppercase:" + str. toUpperCase (); // verify whether the character J exists. int index = str. indexOf ("J"); // returns the subscript if any, and-1 System if no. out. println (index); // the character of the subString from 0 to 4, but does not contain the four-character System. out. println (str. substring (0, 4); // starts from 4 to the end of System. out. println (str. substring (4); // split String email = "wei@163.com; hao@sohu.com; aa@qq.com"; String [] emaillist = email. split (";"); // traverses the array for (String s: emaillist) {System. out. println (s );}}}
Iv. StringBuffer class
Package com. pb. demo1; public class StringBufferTest {public static void main (String [] args) {StringBuffer str = new StringBuffer ("abc_def_add_zzD "); // lookup _ the first occurrence location int indexfirst = str. indexOf ("_"); System. out. println ("location where _ appears for the first time" + indexfirst); // Add "A" str. insert (indexfirst + 1, "A"); System. out. println (str); // search for the last index location where "_" appears. int indexlast = str. lastIndexOf ("_"); System. out. println ("find the index location where the last _ appears" + indexlast); // Delete the last "_" // str. deleteCharAt (indexlast); str. delete (indexlast, indexlast + 1); System. out. println (str); System. out. println ("========================"); StringBuffer email = new StringBuffer ("rock105@sohu.com "); //// search for the index location int index = email. indexOf ("@"); email. replace (0, index, "tom"); System. out. println (email );}}