Java StringBuffer and StringBuilder class, javastringbuffer

Source: Internet
Author: User

Java StringBuffer and StringBuilder class, javastringbuffer

When modifying a string, use the StringBuffer and StringBuilder classes.

Unlike the String class, objects in the StringBuffer and StringBuilder classes can be modified multiple times without generating new unused objects.

StringBuilder class is proposed in Java 5. The biggest difference between StringBuffer and StringBuffer is that the StringBuilder method is not thread-safe (cannot be accessed synchronously ).

As StringBuilder has a speed advantage over StringBuffer, we recommend that you use the StringBuilder class in most cases. However, when applications require thread security, the StringBuffer class must be used.

Instance
public class Test{    public static void main(String args[]){       StringBuffer sBuffer = new StringBuffer(" test");       sBuffer.append(" String Buffer");       System.ou.println(sBuffer);     }}

The above example compilation and running results are as follows:

test String Buffer

 

StringBuffer MethodThe following are the main methods supported by the StringBuffer class:
Serial number Method description
1 Public StringBuffer append (String s)
Append the specified string to this character sequence.
2 Public StringBuffer reverse ()
Replace the Character Sequence with its reverse form.
3 Public delete (int start, int end)
Remove characters from the substring of this sequence.
4 Public insert (int offset, int I)
SetintThe string representation of the parameter is inserted into this sequence.
5 Replace (int start, int end, String str)
Use the givenStringThe character in replaces the character in the substring of this sequence.
The methods in the following list are similar to those of the String class:
Serial number Method description
1 Int capacity ()
Returns the current capacity.
2 Char charAt (int index)
ReturnscharValue.
3 Void ensureCapacity (int minimumCapacity)
Make sure that the capacity is at least equal to the specified minimum value.
4 Void getChars (int srcBegin, int srcEnd, char [] dst, int dstBegin)
Copy characters from this sequence to the target character arraydst.
5 Int indexOf (String str)
Returns the index of the first occurrence of the specified substring in the string.
6 Int indexOf (String str, int fromIndex)
Returns the index of the first occurrence of the specified substring in the string starting from the specified index.
7 Int lastIndexOf (String str)
Returns the index of the substring that appears on the rightmost side of the string.
8 Int lastIndexOf (String str, int fromIndex)
Returns the index of the last occurrence of the specified substring in this string.
9 Int length ()
Returns the length (number of characters ).
10 Void setCharAt (int index, char ch)
Set the character at the given indexch.
11 Void setLength (int newLength)
Set the length of the Character Sequence.
12 CharSequence subSequence (int start, int end)
Returns a new character sequence, which is a subsequence of the sequence.
13 String substring (int start)
Returns a newStringIt contains the character subsequence currently contained in this character sequence.
14 String substring (int start, int end)
Returns a newStringIt contains the character subsequence currently contained in this sequence.
15 String toString ()
Returns the string representation of the data in this sequence.
Address: http://www.manongjc.com/java/java_stringbuffer.html

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.