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
Character
Class 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.
StringBuffer
The main operation on isappend
Andinsert
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.insert
The method adds characters to the specified vertex.
For example, ifz
Reference 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 referencesStringBuilder
For 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 newString
It contains the character subsequence currently contained in this character sequence. String
substring(int start,
int end)
Returns a newString
It contains the character subsequence currently contained in this sequence.
Replace ()
StringBuffer
replace(int start,
int end, String str)
Use the given
String
The character in replaces the character in the substring of this sequence.
Append ()
StringBuffer
append(boolean b)
Setboolean
The string representation of the parameter is appended to the sequence. StringBuffer
append(char c)
Setchar
The string representation of the parameter is appended to this sequence.
StringBuffer
append(char[] str)
Setchar
The 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)
CharSequence
Append to the sequence. StringBuffer
append(CharSequence s, int start,
int end)
SpecifyCharSequence
To this sequence. StringBuffer
append(double d)
Setdouble
The string representation of the parameter is appended to this sequence. StringBuffer
append(float f)
Setfloat
The string representation of the parameter is appended to this sequence. StringBuffer
append(int i)
Setint
The string representation of the parameter is appended to this sequence.
StringBuffer
append(long lng)
Setlong
The string representation of the parameter is appended to this sequence.
StringBuffer
append(Object obj)
AppendObject
Parameter 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)
Setchar
The 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)
SpecifyCharSequence
Insert this sequence. StringBuffer
insert(int dstOffset,
CharSequence s, int start,
int end)
SpecifyCharSequence
Into this sequence. StringBuffer
insert(int offset,
double d)
Setdouble
The string representation of the parameter is inserted into this sequence. StringBuffer
insert(int offset,
float f)
Setfloat
The string representation of the parameter is inserted into this sequence.
StringBuffer
insert(int offset,
int i)
Setint
The string representation of the parameter is inserted into this sequence. StringBuffer
insert(int offset,
long l)
Setlong
The string representation of the parameter is inserted into this sequence.
StringBuffer
insert(int offset,
Object obj)
Set
Object
The 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.