Android StringBuffer and StringBuilder, androidstringbuffer

Source: Internet
Author: User

Android StringBuffer and StringBuilder, androidstringbuffer

If our program runs in a single thread, or we do not need to consider the thread synchronization problem, we should first use the StringBuilder class. To ensure thread security, it is naturally StringBuffer.

Except for the different multithreading support, the usage and results of these two classes are almost identical,

The difference is that StringBufferd supports concurrent operations, linear security, and is suitable for multithreading. StringBuilder does not support concurrent operations. It is linearly insecure and is not suitable for multithreading. The newly introduced StringBuilder class is not thread-safe, but its performance in a single thread is higher than that in StringBuffer.

Common StringBuffer Methods

(Since StringBuffer and StringBuilder are almost the same in usage, write only one file. The following content is collected from different parts of the network and no source is specified)

StringBuffer s = new StringBuffer ();

In this way, the initialized StringBuffer object is an empty object,

StringBuffer sb1 = new StringBuffer (512 );
A 512-byte character buffer is allocated.

StringBuffer sb2 = new StringBuffer ("how are you ?")

Create a StringBuffer object with content, and store the string "how are you?" In the character buffer ?"

 

A. append Method
Public StringBuffer append (boolean B)
This method appends content to the end of the current StringBuffer object, similar to a string connection. After this method is called, the content of the StringBuffer object is also changed. For example:
StringBuffer sb = new StringBuffer ("abc ");
Sb. append (true );
The sb value of the object is changed to "abctrue"

Using this method to connect strings saves more content than strings, and is often applied to the connection of Database SQL statements.

 

B. deleteCharAt Method
Public StringBuffer deleteCharAt (int index)
The function of this method is to delete the characters at the specified position, and then form the remaining content into a new string. For example:
StringBuffer sb = new StringBuffer ("KMing ");
Sb. deleteCharAt (1 );
The purpose of this Code is to delete the character whose index value is 1 in the string object sb, that is, to delete the second character. The remaining content forms a new string. So the sb value of the object is changed to "King ".
There is also a function-like delete method:
Public StringBuffer delete (int start, int end)
This method is used to delete all characters within the specified range, including start, excluding the range of the end index value. For example:
StringBuffer sb = new StringBuffer ("TestString ");
Sb. delete (1, 4 );
The purpose of this Code is to delete all characters between index value 1 (inclusive) and index value 4 (excluded), and the remaining characters form a new string. The sb value of the object is TString ".

 

C. insert Method
Public StringBuffer insert (int offset, boolean B ),
This method inserts content into the StringBuffer object and then forms a new string. For example:
StringBuffer sb = new StringBuffer ("TestString ");
Sb. insert (4, false );
The code in this example inserts the false value at the index value 4 of the object sb to form a new string. After the execution, the sb value of the object is "TestfalseString ".

 

D. reverse Method
Public StringBuffer reverse ()
This method is used to reverse the content in the StringBuffer object and form a new string. For example:
StringBuffer sb = new StringBuffer ("abc ");
Sb. reverse ();
After the inversion, the content in the object sb will become "CBA ".

 

E. setCharAt Method
Public void setCharAt (int index, char ch) the function of this method is to modify the index value in the object to the index position of the new character ch. For example:
StringBuffer sb = new StringBuffer ("abc ");
Sb. setCharAt (1, 'D ');
The sb value of the object is changed to "aDc ".

 

F. trimToSize Method
Public void trimToSize ()
The function of this method is to narrow down the storage space in the StringBuffer object to the same length as the String length, reducing the waste of space. It is the same as the trim () of the String, and is not used as an example.

 

G, length Method
This method is used to obtain the string length.

 

H, setlength Method
This method is used to set the size of the string buffer.
StringBuffer sb = new StringBuffer ();
Sb. setlength (100 );
If the setlength () method is called with a value less than the current string length, the characters after the new length will be lost.

 

I, sb. capacity Method
This method is used to obtain the size of a string.
StringBuffer sb = new StringBuffer ("string ");
Int I = sb. capacity ();

 

J. ensureCapacity Method
This method is used to reset the size of the string.
StringBuffer sb = new StringBuffer ();
Sb. ensureCapacity (32); // pre-set the sb capacity to 32

 

K. getChars Method
This method is used to copy the substring of a string to an array.
GetChars (int start, int end, char chars [], int charStart );

StringBuffer sb = new StringBuffer ("I love You ");
Int begin = 0;
Int end = 5;
// Note that the length of the ch character array must be greater than or equal to the length of the Character Between In in and end
// ArrayIndexOutOfBoundsException will be reported if the value is smaller
// If the value is greater than, the characters greater than the value are filled with spaces.
Char [] ch = new char [end-begin];
Sb. getChars (begin, end, ch, 0 );
System. out. println (ch );

Result: I lov

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.