Difference and connection between string type string stringbuffer

Source: Internet
Author: User


Use the API documentation to view how to use these two classes:

String
Classes can be used to check a single character, compare string, search string, extract substring, create a copy of string, and convert all characters to uppercase or lowercase letters. Case-sensitive ing based on CharacterClass specified Unicode
Standard Edition
String Length ()

 int
length()

Returns the length of this string.
Substring ()

 String
substring(int beginIndex)

Returns a new string, which is a substring of this string.  String
substring(int beginIndex,
int endIndex)

Returns a new string, which is a substring of this string.
Replace


String
replace(char oldChar,
char newChar)

Returns a new string by using newChar
Replace all oldChar.  String
replace(CharSequence target, CharSequence replacement)

Replace all the substrings that match the target sequence of the string with the specified string value.


public final class StringBuffer
Extends object
Implements serializable, charsequence

A variable string of thread-safe characters. AString
But cannot be modified. Although it contains a specific character sequence at any time point, the length and content of the sequence can be changed by calling some methods.

The string buffer can be safely used for multiple threads. These methods can be synchronized as necessary, so all the operations on any specific instance are in serial order, this sequence is consistent with the method call sequence of each involved thread.

StringBufferThe main operation on isappendAndinsert
Methods to accept any type of data. Each method can effectively convert the given data to a string, and then append or insert the character of the string into the string buffer.append
The method always adds these characters to the end of the buffer.insertThe method adds characters to the specified vertex.

For example, ifzReference the current content"start"String buffer object, this method is called
z.append("le")Causes the string buffer to contain"startle", And
z.insert(4, "le")Will change the string buffer to include"starlet".

Generally, if Sb referencesStringBuilderFor examplesb.append(x)And
sb.insert(sb.length(), x)With the same effect.

When an operation related to the source sequence (such as an append or insert operation in the source sequence) occurs, the operation is only performed on the string buffer for this operation, rather than on the source.

Each string buffer has a certain capacity. As long as the length of the Character Sequence contained in the string buffer does not exceed this capacity, no new internal buffer array needs to be allocated. If the internal buffer overflow occurs, the capacity increases automatically. Slave
In JDK 5, an equivalent class used by a single thread is added to this class, that isStringBuilder. Compared with this class, it is usually preferred to use
StringbuilderClass, because it supports all the same operations, but because it does not execute synchronization, so the speed is faster.


Stringbuffer


Length ()
int
length()

Returns the length (number of characters ).
Substring ()

 String
substring(int start)

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

Returns a newStringIt contains the character subsequence currently contained in this sequence.
Replace ()
StringBuffer
replace(int start,
int end, String str)

Use the given
StringThe character in replaces the character in the substring of this sequence.
Append ()

StringBuffer
append(boolean b)

SetbooleanThe string representation of the parameter is appended to the sequence. StringBuffer
append(char c)

SetcharThe string representation of the parameter is appended to this sequence.
 StringBuffer
append(char[] str)

SetcharThe string representation of the array parameter is appended to this sequence.StringBuffer
append(char[] str,
int offset, int len)
Setchar
The string representation of the sub-array of the array parameter is appended to this sequence. StringBuffer
append(CharSequence s)

CharSequenceAppend to the sequence. StringBuffer
append(CharSequence s, int start,
int end)

SpecifyCharSequence
To this sequence. StringBuffer
append(double d)

SetdoubleThe string representation of the parameter is appended to this sequence. StringBuffer
append(float f)

SetfloatThe string representation of the parameter is appended to this sequence. StringBuffer
append(int i)

SetintThe string representation of the parameter is appended to this sequence.
 StringBuffer
append(long lng)

SetlongThe string representation of the parameter is appended to this sequence.
 StringBuffer
append(Object obj)

AppendObjectParameter string representation. StringBuffer
append(String str)

Append the specified string to this character sequence. StringBuffer
append(StringBuffer sb)

StringbufferAppend to this sequence.
Insert ()

 StringBuffer
insert(int offset,
boolean b)

Setboolean
The string representation of the parameter is inserted into this sequence. StringBuffer
insert(int offset,
char c)

SetcharThe string representation of the parameter is inserted into this sequence.
 StringBuffer
insert(int offset,
char[] str)

Setchar
The string representation of the array parameter is inserted into this sequence.
 StringBuffer
insert(int index,
char[] str, int offset, int len)

Convert array parametersstr
The string representation of the sub-array is inserted into this sequence. StringBuffer
insert(int dstOffset,
CharSequence s)


SpecifyCharSequenceInsert this sequence. StringBuffer
insert(int dstOffset,
CharSequence s, int start,
int end)

SpecifyCharSequence
Into this sequence. StringBuffer
insert(int offset,
double d)

SetdoubleThe string representation of the parameter is inserted into this sequence. StringBuffer
insert(int offset,
float f)

SetfloatThe string representation of the parameter is inserted into this sequence.
 StringBuffer
insert(int offset,
int i)

SetintThe string representation of the parameter is inserted into this sequence. StringBuffer
insert(int offset,
long l)

SetlongThe string representation of the parameter is inserted into this sequence.
 StringBuffer
insert(int offset,
Object obj)

Set
ObjectThe string representation of the parameter is inserted into this character sequence. StringBuffer
insert(int offset,
String str)


Inserts a string into this character sequence.

Delete ()
 StringBuffer
delete(int start,
int end)

Remove characters from the substring of this sequence.
Reverse ()
StringBuffer
reverse()

Replace the Character Sequence with its reverse form.

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.