Android StringBuffer and StringBuilder

Source: Internet
Author: User

If our program runs on a single thread, or does not have to take into account thread synchronization issues, we should prioritize the use of the StringBuilder class; If you want to ensure thread safety, it is stringbuffer.

In addition to support for multithreading, the use of these two classes and the results of almost no difference,

The difference is that Stringbufferd supports concurrent operations, linear security, and is suitable for use in multi-threading. StringBuilder does not support concurrent operations, which are linearly unsafe and not suitable for use in multi-threading. The newly introduced StringBuilder class is not thread-safe, but its performance in a single thread is higher than stringbuffer.

StringBuffer Common methods

(Because StringBuffer and StringBuilder are almost the same in use, so write only one, the following parts of the network collected everywhere, no longer mark the source)

StringBuffer s = new StringBuffer ();

This initializes the StringBuffer object as an empty object,

StringBuffer sb1=new StringBuffer (512);
A character buffer with a length of 512 bytes is allocated.

StringBuffer sb2=new StringBuffer ("How is You?")

Creates a StringBuffer object with content that holds the string in the character buffer "How is it?"

A, Append method
Public StringBuffer Append (Boolean B)
The function of this method is to append the content to the end of the current StringBuffer object, similar to a string connection, and after the method is called, the contents of the StringBuffer object are changed, for example:
StringBuffer sb = new StringBuffer ("abc");
Sb.append (TRUE);
Then the value of the object SB will become "Abctrue"

Using this method to concatenate strings is more economical than string 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 character 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 function of the code is to delete the string object SB in the index value of 1 characters, that is, to delete the second character, the remaining content constitutes a new string. So the value of the object SB becomes "King".
There is also a function-like Delete method:
Public StringBuffer Delete (int start,int end)
The function of this method is to delete all characters within the specified interval, including the start, which does not contain the range of the end index value. For example:
StringBuffer sb = new StringBuffer ("teststring");
SB. Delete (1,4);
The purpose of the code is to remove all characters between the index value 1 (including) to the index value 4 (not included), and the remaining characters to form a new string. The value of the object SB is "Tstring".

C, Insert method
Public stringbuffer Insert (int offset, Boolean b),
The function of this method is to insert content into the StringBuffer object and then form a new string. For example:
StringBuffer sb = new StringBuffer ("teststring");
Sb.insert (4,false);
The purpose of the sample code is to insert a false value at the index value 4 of the object SB, to form a new string, then the value of the object SB after execution is "testfalsestring".

D, Reverse method
Public StringBuffer Reverse ()
The function of this method is to invert the contents of the StringBuffer object and then form a new string. For example:
StringBuffer sb = new StringBuffer ("abc");
Sb.reverse ();
After the reversal, the content in the object SB becomes "CBA".

E, Setcharat method
public void Setcharat (int index, char ch) The function of this method is to modify the character of the index value in the object to be the new character Ch. For example:
StringBuffer sb = new StringBuffer ("abc");
Sb.setcharat (1, ' D ');
The value of the object SB will become "ADc".

F, TrimToSize method
public void TrimToSize ()
The function of this method is to reduce the storage space of the StringBuffer object to the same length as the length of the string, reducing the waste of space and the same effect as the trim () of the string, not for example.

G, Length method
The function of this method is to get the length of the string, no need to say it.

H, SetLength method
The function of this method is to set the string buffer size.
StringBuffer sb=new StringBuffer ();
Sb.setlength (100);
If you call the SetLength () method with a value that is less than the current string length, the characters following the new length are lost.

I, Sb.capacity method
The function of this method is to get the capacity of the string.
StringBuffer sb=new StringBuffer ("string");
int i=sb.capacity ();

J, Ensurecapacity Method
The function of this method is to reset the size of the string capacity.
StringBuffer sb=new StringBuffer ();
Sb.ensurecapacity (32); Pre-set SB capacity of 32

K, GetChars method
The function of this method is to copy the substring of the string to the 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 Begin and end
Less than the words will be reported arrayindexoutofboundsexception
If it is greater than, the characters greater than will be padded with spaces
char[] ch = new Char[end-begin];
Sb.getchars (Begin, end, CH, 0);
SYSTEM.OUT.PRINTLN (CH);

Results: I Lov

Android StringBuffer and StringBuilder

Related Article

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.