Java StringBuffer and StringBuilder classes

Source: Internet
Author: User
Tags stringbuffer

Java StringBuffer and StringBuilder classes

When you modify a string, you need to use the StringBuffer and StringBuilder classes.

Unlike the string class, objects of the StringBuffer and StringBuilder classes can be modified more than once, and no new unused objects are produced.

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

Since StringBuilder has a speed advantage over StringBuffer, it is recommended to use the StringBuilder class in most cases. However, in cases where the application requires thread safety, 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.out.println(sBuffer);     }}

The results of the above example compilation run as follows:

test String Buffer
StringBuffer method

The following are the main methods supported by the StringBuffer class:

Serial Number Method Description
1 Public StringBuffer Append (String s)
Appends the specified string to this sequence of characters.
2 Public StringBuffer Reverse ()
This sequence of characters is replaced with its inverse form.
3 Public delete (int start, int end)
Removes the characters in the substring of this sequence.
4 public insert (int offset, int i)
intinserts the string representation of a parameter into this sequence.
5 Replace (int start, int end, String str)
Replaces the String characters in the substring of this sequence with the characters given.

The methods in the following list are similar to the methods of the string class:

Serial number Method description
1 int capacity ()
Returns the current capacity.
2 char charAt (int index)
Returns the value at the specified index in this sequence char .
3 void ensurecapacity (int minimumcapacity)
Ensure that the capacity is at least equal to the specified minimum value.
4 void GetChars (int srcbegin, int srcend, char[] DST, int dstbegin)
Copies the character from this sequence to the target character array dst .
5 int indexOf (String str)
Returns the index of the specified substring that appears for the first time 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 at the specified index.
7 int lastIndexOf (String str)
Returns the index to the rightmost occurrence of the specified substring in this 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)
Sets the character at the given index to ch .
11 void setLength (int newlength)
Sets the length of the character sequence.
12 Charsequence subsequence (int start, int end)
Returns a new sequence of characters that is a subsequence of this sequence.
13 String substring (int start)
Returns a new that String contains the sequence of characters that this character sequence currently contains.
14 String substring (int start, int end)
Returns a new String containing sequence of characters that this sequence currently contains.
15 String toString ()
Returns a string representation of the data in this sequence.

Java StringBuffer and StringBuilder classes

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.