When modifying a string, use the StringBuffer and StringBuilder classes.
Unlike the String class, objects of the StringBuffer and StringBuilder classes can be modified multiple times, and new unused objects are not generated.
The StringBuilder class was introduced in Java 5 and the biggest difference between it and StringBuffer is that StringBuilder methods are not thread-safe (synchronous access is not possible).
Because StringBuilder has a speed advantage over StringBuffer, the StringBuilder class is recommended for most situations. However, if the application requires thread safety, you must use the StringBuffer class.
Test.java file code:
public class Test{
public static void main(String args[]){
StringBuffer sBuffer = new StringBuffer("Rookie tutorial official website:");
sBuffer.append("www"); sBuffer.append(".interviewbubble"); sBuffer.append(".com"); System.out.println(sBuffer); } }
sBuffer.append("www"); sBuffer.append(".interviewbubble"); sBuffer.append(".com"); System.out.println(sBuffer); } }
The above example compiled and run results are as follows:
Rookie tutorial official website: www.interviewbubble.com
StringBuffer methods
The following are the main methods supported by the StringBuffer class:
No. | Method description |
---|---|
1 | Public StringBuffer append(String s) Appends the specified string to this character sequence. |
2 | Public StringBuffer reverse() Replaces this character sequence with its inverted form. |
3 | Public delete(int start, int end) Removes the characters in the substring of this sequence. |
4 | public insert (int offset, int i ) the int string representation of the argument into this sequence. |
5 | replace (int start, int end, String str) given String characters replacing substrings of this sequence of characters. |
The methods in the following list are similar to those of the String class:
No. | Method description |
---|---|
1 | Int capacity() Returns the current capacity. |
2 | char charAt (int index) Returns this sequence at the specified index char value. |
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) Copies 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 this string. |
6 | Int indexOf(String str, int fromIndex) Returns the index of the first occurrence of the specified substring in this string, starting at the specified index. |
7 | Int lastIndexOf(String str) Returns the index of the specified right-most substring in this string. |
8 | Int lastIndexOf(String str, int fromIndex) Returns the last occurrence of the substring in the String object. |
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 character sequence that is a subsequence of this sequence. |
13 | String substring(int start) returns a new String containing the current subsequence of characters in this character sequence. |
14 | String substring(int start, int end) returns a new String containing the current subsequence of characters in this sequence. |
15 | String toString() Returns a string representation of the data in this sequence. |
Comments
Post a Comment