Home > Java > javaTutorial > body text

[java tutorial] Java StringBuffer and StringBuilder classes

黄舟
Release: 2016-12-26 13:17:36
Original
1304 people have browsed it

Java StringBuffer and StringBuilder classes


When modifying strings, you need to use the StringBuffer and StringBuilder classes.

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

The StringBuilder class was proposed in Java 5. 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 compared to StringBuffer, it is recommended to use the StringBuilder class in most cases. However, when the application requires thread safety, the StringBuffer class must be used.

Examples

public class Test{

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

The compilation and running results of the above examples are as follows:

test String Buffer
Copy after login

StringBuffer methods

The 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()
Replaces this character sequence with its reversed form.

3 public delete(int start, int end)
Remove characters in the substring of this sequence.

4 public insert(int offset, int i)
Insert the string representation of the int parameter into this sequence.

5 replace(int start, int end, String str)
Replace the characters in the substring of this sequence with the characters in the given String.

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 char value at the specified index in this sequence.

3 void ensureCapacity(int minimumCapacity)
Ensures 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 array dst.

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)
Starting from the specified index, return the index of the first occurrence of the specified substring in the string.

7 int lastIndexOf(String str)
Returns the index of 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)
Set the character at the given index to ch.

11 void setLength(int newLength)
Set the length of the character sequence.

12 CharSequence subSequence(int start, int end)
returns a new character sequence that is a subsequence of this sequence.

13 String substring(int start)
Returns a new String that contains the character subsequence currently contained in this character sequence.

14 String substring(int start, int end)
Returns a new String that contains the character subsequence currently contained in this sequence.

15 String toString()
Returns the string representation of the data in this sequence.

The above is the content of [java tutorial] Java StringBuffer and StringBuilder classes. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template