|
一, 字串數組
建構函式:
String[] array = new String[5];
String[] array =new String[] {“aa”,”bb”,”cc”};
String[] array = {“aa”,”bb”,”cc”};
字串數組也被看做對象:
屬性:length (區別於字串的方法length())
常用方法:void System.arraycopy(object[] from,intfromIndex, object[] to,int fromIndex,int count),系統的一個靜態方法
二,字串緩衝類StringBuffer
String類對象一旦聲明就不能改變,StringBuffer緩衝類實現了可變字元序列。
StringBuffer上的基本操作是append和insert方法,它們接受任意的資料,把所給資料轉化為一個字串,並把這個字串添加到緩衝區的末端或者插到指定地方。
public final class StringBuffer
extends Object
implements Serializable, CharSequence
|
構造方法摘要 |
|
StringBuffer()
構造一個其中不帶字元的字串緩衝區,初始容量為 16 個字元。 |
|
|
StringBuffer(CharSequence seq)
public java.lang.StringBuilder(CharSequence seq) 構造一個字串緩衝區,它包含與指定的 CharSequence 相同的字元。 |
|
|
StringBuffer(int capacity)
構造一個不帶字元,但具有指定初始容量的字串緩衝區。 |
|
|
StringBuffer(String str)
構造一個字串緩衝區,並將其內容初始化為指定的字串內容。 |
|
其他方法:
StringBuffer轉化為String對象:toString()
|
String |
toString()
Returns a string representing the data in this sequence. |
StringBuffer的擴充appand():
|
StringBuffer |
append(boolean b)
Appends the string representation of the boolean argument to the sequence. |
|
StringBuffer |
append(char c)
Appends the string representation of the char argument to this sequence. |
|
StringBuffer |
append(char[] str)
Appends the string representation of the char array argument to this sequence. |
|
StringBuffer |
append(char[] str, int offset, int len)
Appends the string representation of a subarray of the char array argument to this sequence. |
|
StringBuffer |
append(CharSequence s)
將指定的 CharSequence 添加到該序列。 |
|
StringBuffer |
append(CharSequence s, int start, int end)
Appends a subsequence of the specified CharSequence to this sequence. |
等等…….
任意類型都將被轉化為字串加入緩衝末端
StringBuffer的插入insert():
|
StringBuffer |
insert(int offset, Object obj)
Inserts the string representation of the Object argument into this character sequence. |
|
StringBuffer |
insert(int offset, String str)
Inserts the string into this character sequence. |
|
StringBuffer |
insert(int offset, char[] str)
Inserts the string representation of the char array argument into this sequence. |
|
StringBuffer |
insert(int index, char[] str, int offset, int len)
Inserts the string representation of a subarray of the str array argument into this sequence. |
等等……
典型的其他動作:
|
int |
length()
Returns the length (character count). |
|
char |
charAt(int index)
Returns the char value in this sequence at the specified index. |
|
String |
substring(int start)
Returns a new String that contains a subsequence of characters currently contained in this character sequence. |
|
int |
indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring. |
|
StringBuffer |
replace(int start, int end, String str)
Replaces the characters in a substring of this sequence with characters in the specified String. |
|
StringBuffer |
delete(int start, int end)
Removes the characters in a substring of this sequence. |
等等….. |