StringBuffer: String buffer
Features: The composition of the string is achieved through this class.
StringBuffer can be used to delete the contents of the string.
StringBuffer is a container.
Many methods are the same as string.
The StringBuffer is variable-length.
StringBuffer can store different types of data.
Eventually you want to turn it into a string.
Since it is a container object, what function should it have?
1. Add:
StringBuffer append (date);
StringBuffer Insert (index, date)
2. Delete:
StringBuffer Delete (start, end); Contains a header, does not contain a tail.
StringBuffer deletecharat (int index): Deletes the element at the specified position. StringBuffer Delete (0, the length of the buffer);
3. Find:
Char charAt (index);
int indexOf (String);
int lastIndexOf (string);
4, modify:
StringBuffer replace (start, end, String);
void Setcharat (index, String);
General additions and deletions Change C (Create) U (update) R (read) D (delete)
Set Length:
void setLength (int newlength);
Construction Method:
Variable-length arrays:
StringBuffer (int capacity)
PackageCn.itcast.p2.stringbuffer.demo; Public classStringbufferdemo {/** * @paramargs*/ Public Static voidMain (string[] args) {/** StringBuffer: Is the string buffer. * A container for storing data. * Features: * 1, variable length. * 2, you can store different types of data. * 3, eventually to be converted into a string for use. * 4, you can modify the string. * * * * Since it is a container object. What functions should be available? * 1, add: * stringbuffer append (data); * StringBuffer Insert (index,data); * 2, Delete: * StringBuffer Delete (start,end): Contains header, does not contain tail. * StringBuffer deletecharat (int index): Delete the element at the specified position * 3, look for: * char charAt (index); * int indexOf (string); * int lastIndexOf (string); * 4, Modified: * StringBuffer replace (start,end,string); * void Setcharat (Index,char); * * Delete and change to C (create) U (update) R (read) D (delete) * * **/buffermethoddemo_2 (); } Private Static voidbuffermethoddemo_2 () {StringBuffer sb=NewStringBuffer ("ABCE"); //Sb.delete (1, 3);//AE//clears the buffer. //Sb.delete (0,sb.length ());//sb = new StringBuffer (); //sb.replace (1, 3, "NBA");//Sb.setcharat (2, ' Q '); //Sb.setlength (10); //System.out.println ("SB:" +SB);//System.out.println ("Len:" +sb.length ());System.out.println (Sb.reverse ()); } Private Static voidbuffermethoddemo_1 () {StringBuffer sb=NewStringBuffer ("ABCE"); //sb.append ("Xixi");Sb.insert (2, "QQ"); System.out.println (Sb.tostring ()); } Public Static voidBuffermethoddemo () {//creates a buffer object. StringBuffer SB =NewStringBuffer (); Sb.append (4). Append (false);//. Append ("haha");Sb.insert (1, "haha");//Sb.append (true);System.out.println (SB); } }
Common Object API (StringBuffer Class)