StringBuffer Connection character operation use StringBuffer to use the Append () method in StringBuffer when the contents of a string need to be changed frequently, to complete the connection operation of the string two,Common methods of StringBuffer class
No. |
Method definition |
Type |
Describe |
1 |
Public StringBuffer () |
Structure |
The construction method of StringBuffer |
2 |
Public StringBuffer append (char c) |
Method |
A number of append operations are provided in StringBuffer (similar to using "+" in string), and you can append content to StringBuffer, which can add any data type. |
3 |
Public StringBuffer append (String str) |
Method |
4 |
Public StringBuffer append (StringBuffer sb) |
Method |
5 |
public int indexOf (String str) |
Method |
Finds whether the specified string exists |
6 |
public int indexOf (String str,int fromIndex) |
Method |
Finds whether the specified string exists, starting at the specified position |
7 |
Public stringbuffer Insert (int offset,string str) |
Method |
Adds the specified string at the specified position |
8 |
Public StringBuffer Reverse () |
Method |
Invert content to save |
9 |
Public stringbuffer replace (int start,int end,string str) |
Method |
Specify content Substitution |
10 |
public int Length () |
Method |
Find the content length |
11 |
Public StringBuffer Delete (int start,int end) |
Method |
Delete a string of the specified range |
12 |
Public String substring (int start) |
Method |
String intercept, specifying the start point |
13 |
Public String substring (int start,int end) |
Method |
Intercepts a specified range of strings |
14 |
Public String toString () |
Method |
Method inherited by the object class to change the content to a string type |
Third, the use of the string class
PackageCom.pb.demo1; Public classStringtest { Public Static voidMain (string[] args) {String str= "To learn java! well"; //length Long () methodThe length of the System.out.println (str+ ") is:" +str.length ()); //convert Case toLowerCase (), toUpperCase ()System.out.println ("Convert to lowercase:" +str.tolowercase ()); System.out.println ("Convert to uppercase:" +str.touppercase ()); //Verify that there is a J character intIndex=str.indexof ("J"); //return the subscript, no return-1System.out.println (index); //substring characters from 0 to 4 but not with subscript 4System.out.println (str.substring (0, 4)); //from the beginning to the end of the subscript 4System.out.println (Str.substring (4)); //SplitString email= "[email protected]; [Email protected]; [Email protected] "; String [] emaillist=email.split (";"); //iterating through an array for(String s:emaillist) {System.out.println (s); } }}
Four, StringBuffer class
PackageCom.pb.demo1; Public classStringbuffertest { Public Static voidMain (string[] args) {stringbuffer str=NewStringBuffer ("Abc_def_add_zzd"); //find the first occurrence of the position intIndexfirst=str.indexof ("_"); System.out.println ("Find where _ first appears" +Indexfirst); //Add "A" to the back of the first "_"Str.insert (indexfirst+1, "A"); System.out.println (str); //find the index position of the last occurrence of "_" intIndexlast=str.lastindexof ("_"); System.out.println ("Find index location of last occurrence _" +indexlast); //Delete Last "_"//Str.deletecharat (indexlast);Str.delete (Indexlast, indexlast+1); System.out.println (str); System.out.println ("==================="); StringBuffer Email=NewStringBuffer ("[Email protected]"); ////find the index location for "@" intIndex=email.indexof ("@"); Email.replace (0, index, "Tom"); SYSTEM.OUT.PRINTLN (email); }}
Java starts from zero 30 (string and StringBuffer classes)