1 Overview of the StringBuffer1.1 StringBuffer
- If we need to stitch the strings, each stitch will build a new string object, which is time consuming and wasted space. And stringbuffer can solve this problem.
- StringBuffer is a thread-safe sequence of characters.
1.2 Construction methods
- Construction method: No-parameter construction method
Public StringBuffer ()
- Construction Method: Specifies the capacity of the string buffer
Public StringBuffer (int capacity)
- Constructor method: Specify string contents of a string buffer
Public StringBuffer (String str)
PackageCom.xuweiwei; Public classStringbufferdemo { Public Static voidMain (string[] args) {//Public StringBuffer ()StringBuffer SB =NewStringBuffer (); System.out.println ("SB:" +SB); System.out.println ("SB's Capacity:" +sb.capacity ()); System.out.println ("SB's Length:" +sb.length ()); //Public stringbuffer (int capacity)StringBuffer SB2 =NewStringBuffer (50); System.out.println ("SB2:" +SB2); System.out.println ("Capacity of SB2:" +sb2.capacity ()); System.out.println ("Length of SB2:" +sb2.length ()); //Public StringBuffer (String str)StringBuffer SB3 =NewStringBuffer ("Hello"); System.out.println ("SB3:" +SB3); System.out.println ("Capacity of SB3:" +sb3.capacity ()); System.out.println ("Length of SB3:" +sb3.length ()); }}1.3 StringBuffer Add-on features
- Method: You can add any type of data to the string buffer and return the string buffer itself, which has overloaded methods
Public StringBuffer append (String str)
- Method: Inserts any type of data into the string buffer at the specified location and returns the string buffer itself, which has overloaded methods
Public StringBuffer Insert (int offset,string str)
PackageCom.xuweiwei; Public classStringBufferDemo2 { Public Static voidMain (string[] args) {StringBuffer sb=NewStringBuffer (); //Public stringbuffer Append (String str)Sb.append ("Hello"); Sb.append (true); Sb.append (12); Sb.append (A); System.out.println ("SB:" +SB); Sb.insert (0, ' d '); System.out.println ("SB:" +SB); }}1.4 StringBuffer Removal function
- Method: Delete The character at the specified position, note that the deletion is one, and return the string buffer itself
Public StringBuffer Deletecharat (int index)
- Method: Deletes the content within the specified range and returns the string buffer itself
Public StringBuffer Delete (int start,int end)
PackageCom.xuweiwei; Public classStringBufferDemo2 { Public Static voidMain (string[] args) {StringBuffer sb=NewStringBuffer (); Sb.append ("Hello"); Sb.append (true); Sb.append (12); Sb.append (A); Sb.insert (0, ' d '); System.out.println ("SB:" +SB); Sb.deletecharat (0); System.out.println ("SB:" +SB); Sb.delete (The); System.out.println ("SB:" +SB); }}1.5 StringBuffer Replacement function
- Method: From start to end, replace with STR, return the string buffer object
Public StringBuffer replace (intint end,string str)
Package Com.xuweiwei; Public class StringBufferDemo2 { publicstaticvoid main (string[] args) { New StringBuffer (); Sb.append ("Hello"). Append ("World"). Append ("Java"); Sb.replace (5, 10, "Hello, World"); System.out.println ("SB:" + SB); }}
1.6 StringBuffer reversal function
Public StringBuffer reverse ()
Package Com.xuweiwei; Public class StringBufferDemo2 { publicstaticvoid main (string[] args) { New stringbuffer (); Sb.append ("Hello"). Append ("World"). Append ("Java"); System.out.println ("SB:" + Sb.reverse ());} }
interception function of 1.7 stringbuffer
- Method: Intercepts the content from the specified position, returning the string object
Public String substring (int start)
- Method: Truncate from the specified position to the end of the specified position, returning the string object
Public String substring (int start,int end)
Package Com.xuweiwei; Public class StringBufferDemo2 { publicstaticvoid main (string[] args) { New StringBuffer (); Sb.append ("Hello"). Append ("World"). Append ("Java"); System.out.println ("SB:" + SB); = sb.substring (3); System.out.println ("SB:" + SB); SYSTEM.OUT.PRINTLN ("str:" + str);} }
Java Fundamentals 12