當前位置:成語大全網 - 新華字典 - java中String和StringBuffer怎麽用

java中String和StringBuffer怎麽用

壹下來自J2SE6.0 API 選了部分常用的

String

構造方法摘要

String()

初始化壹個新創建的 String 對象,使其表示壹個空字符序列。

String(byte[] bytes)

通過使用平臺的默認字符集解碼指定的 byte 數組,構造壹個新的 String。

String(byte[] bytes, Charset charset)

通過使用指定的 charset 解碼指定的 byte 數組,構造壹個新的 String。

String(byte[] ascii, int hibyte)

已過時。 該方法無法將字節正確地轉換為字符。從 JDK 1.1 開始,完成該轉換的首選方法是使用帶有 Charset、字符集名稱,或使用平臺默認字符集的 String 構造方法。

String(byte[] bytes, int offset, int length)

通過使用平臺的默認字符集解碼指定的 byte 子數組,構造壹個新的 String。

String(byte[] bytes, int offset, int length, Charset charset)

通過使用指定的 charset 解碼指定的 byte 子數組,構造壹個新的 String。

String(byte[] ascii, int hibyte, int offset, int count)

已過時。 該方法無法將字節正確地轉換為字符。從 JDK 1.1 開始,完成該轉換的首選方法是使用帶有 Charset、字符集名稱,或使用平臺默認字符集的 String 構造方法。

String(byte[] bytes, int offset, int length, String charsetName)

通過使用指定的字符集解碼指定的 byte 子數組,構造壹個新的 String。

String(byte[] bytes, String charsetName)

通過使用指定的 charset 解碼指定的 byte 數組,構造壹個新的 String。

String(char[] value)

分配壹個新的 String,使其表示字符數組參數中當前包含的字符序列。

String(char[] value, int offset, int count)

分配壹個新的 String,它包含取自字符數組參數壹個子數組的字符。

String(int[] codePoints, int offset, int count)

分配壹個新的 String,它包含 Unicode 代碼點數組參數壹個子數組的字符。

String(String original)

初始化壹個新創建的 String 對象,使其表示壹個與參數相同的字符序列;換句話說,新創建的字符串是該參數字符串的副本。

String(StringBuffer buffer)

分配壹個新的字符串,它包含字符串緩沖區參數中當前包含的字符序列。

String(StringBuilder builder)

分配壹個新的字符串,它包含字符串生成器參數中當前包含的字符序列。

方法摘要

char charAt(int index)

返回指定索引處的 char 值。

int codePointAt(int index)

返回指定索引處的字符(Unicode 代碼點)。

int codePointBefore(int index)

返回指定索引之前的字符(Unicode 代碼點)。

int codePointCount(int beginIndex, int endIndex)

返回此 String 的指定文本範圍中的 Unicode 代碼點數。

int compareTo(String anotherString)

按字典順序比較兩個字符串。

int compareToIgnoreCase(String str)

按字典順序比較兩個字符串,不考慮大小寫。

String concat(String str)

將指定字符串連接到此字符串的結尾。

boolean contains(CharSequence s)

當且僅當此字符串包含指定的 char 值序列時,返回 true。

boolean contentEquals(CharSequence cs)

將此字符串與指定的 CharSequence 比較。

boolean contentEquals(StringBuffer sb)

將此字符串與指定的 StringBuffer 比較。

static String copyValueOf(char[] data)

返回指定數組中表示該字符序列的 String。

static String copyValueOf(char[] data, int offset, int count)

返回指定數組中表示該字符序列的 String。

boolean endsWith(String suffix)

測試此字符串是否以指定的後綴結束。

boolean equals(Object anObject)

將此字符串與指定的對象比較。

boolean equalsIgnoreCase(String anotherString)

將此 String 與另壹個 String 比較,不考慮大小寫。

static String format(Locale l, String format, Object... args)

使用指定的語言環境、格式字符串和參數返回壹個格式化字符串。

static String format(String format, Object... args)

使用指定的格式字符串和參數返回壹個格式化字符串。

byte[] getBytes()

使用平臺的默認字符集將此 String 編碼為 byte 序列,並將結果存儲到壹個新的 byte 數組中。

byte[] getBytes(Charset charset)

使用給定的 charset 將此 String 編碼到 byte 序列,並將結果存儲到新的 byte 數組。

byte[] getBytes(String charsetName)

使用指定的字符集將此 String 編碼為 byte 序列,並將結果存儲到壹個新的 byte 數組中。

void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

將字符從此字符串復制到目標字符數組。

int indexOf(int ch)

返回指定字符在此字符串中第壹次出現處的索引。

int indexOf(int ch, int fromIndex)

返回在此字符串中第壹次出現指定字符處的索引,從指定的索引開始搜索。

int indexOf(String str)

返回指定子字符串在此字符串中第壹次出現處的索引。

int indexOf(String str, int fromIndex)

返回指定子字符串在此字符串中第壹次出現處的索引,從指定的索引開始。

String intern()

返回字符串對象的規範化表示形式。

boolean isEmpty()

當且僅當 length() 為 0 時返回 true。

int lastIndexOf(int ch)

返回指定字符在此字符串中最後壹次出現處的索引。

int lastIndexOf(int ch, int fromIndex)

返回指定字符在此字符串中最後壹次出現處的索引,從指定的索引處開始進行反向搜索。

int lastIndexOf(String str)

返回指定子字符串在此字符串中最右邊出現處的索引。

int lastIndexOf(String str, int fromIndex)

返回指定子字符串在此字符串中最後壹次出現處的索引,從指定的索引開始反向搜索。

int length()

返回此字符串的長度。

boolean matches(String regex)

告知此字符串是否匹配給定的正則表達式。

String replace(char oldChar, char newChar)

返回壹個新的字符串,它是通過用 newChar 替換此字符串中出現的所有 oldChar 得到的。

String replace(CharSequence target, CharSequence replacement)

使用指定的字面值替換序列替換此字符串所有匹配字面值目標序列的子字符串。

String replaceAll(String regex, String replacement)

使用給定的 replacement 替換此字符串所有匹配給定的正則表達式的子字符串。

String replaceFirst(String regex, String replacement)

使用給定的 replacement 替換此字符串匹配給定的正則表達式的第壹個子字符串。

String[] split(String regex)

根據給定正則表達式的匹配拆分此字符串。

String[] split(String regex, int limit)

根據匹配給定的正則表達式來拆分此字符串。

boolean startsWith(String prefix)

測試此字符串是否以指定的前綴開始。

boolean startsWith(String prefix, int toffset)

測試此字符串從指定索引開始的子字符串是否以指定前綴開始。

String substring(int beginIndex)

返回壹個新的字符串,它是此字符串的壹個子字符串。

String substring(int beginIndex, int endIndex)

返回壹個新字符串,它是此字符串的壹個子字符串。

char[] toCharArray()

將此字符串轉換為壹個新的字符數組。

String toLowerCase()

使用默認語言環境的規則將此 String 中的所有字符都轉換為小寫。

String toLowerCase(Locale locale)

使用給定 Locale 的規則將此 String 中的所有字符都轉換為小寫。

String toUpperCase()

使用默認語言環境的規則將此 String 中的所有字符都轉換為大寫。

String toUpperCase(Locale locale)

使用給定 Locale 的規則將此 String 中的所有字符都轉換為大寫。

String trim()

返回字符串的副本,忽略前導空白和尾部空白。

static String valueOf(boolean b)

返回 boolean 參數的字符串表示形式。

static String valueOf(char c)

返回 char 參數的字符串表示形式。

static String valueOf(char[] data)

返回 char 數組參數的字符串表示形式。

static String valueOf(char[] data, int offset, int count)

返回 char 數組參數的特定子數組的字符串表示形式。

static String valueOf(double d)

返回 double 參數的字符串表示形式。

static String valueOf(float f)

返回 float 參數的字符串表示形式。

static String valueOf(int i)

返回 int 參數的字符串表示形式。

static String valueOf(long l)

返回 long 參數的字符串表示形式。

static String valueOf(Object obj)

返回 Object 參數的字符串表示形式。

StringBuffer

構造方法摘要

StringBuffer()

構造壹個其中不帶字符的字符串緩沖區,其初始容量為 16 個字符。

StringBuffer(int capacity)

構造壹個不帶字符,但具有指定初始容量的字符串緩沖區。

StringBuffer(String str)

構造壹個字符串緩沖區,並將其內容初始化為指定的字符串內容。

方法摘要

StringBuffer append(boolean b)

將 boolean 參數的字符串表示形式追加到序列。

StringBuffer append(char c)

將 char 參數的字符串表示形式追加到此序列。

StringBuffer append(char[] str)

將 char 數組參數的字符串表示形式追加到此序列。

StringBuffer append(char[] str, int offset, int len)

將 char 數組參數的子數組的字符串表示形式追加到此序列。

StringBuffer append(double d)

將 double 參數的字符串表示形式追加到此序列。

StringBuffer append(float f)

將 float 參數的字符串表示形式追加到此序列。

StringBuffer append(int i)

將 int 參數的字符串表示形式追加到此序列。

StringBuffer append(long lng)

將 long 參數的字符串表示形式追加到此序列。

StringBuffer append(Object obj)

追加 Object 參數的字符串表示形式。

StringBuffer append(String str)

將指定的字符串追加到此字符序列。

StringBuffer append(StringBuffer sb)

將指定的 StringBuffer 追加到此序列中。

StringBuffer appendCodePoint(int codePoint)

將 codePoint 參數的字符串表示形式追加到此序列。

int capacity()

返回當前容量。

char charAt(int index)

返回此序列中指定索引處的 char 值。

int codePointAt(int index)

返回指定索引處的字符(統壹代碼點)。

int codePointBefore(int index)

返回指定索引前的字符(統壹代碼點)。

int codePointCount(int beginIndex, int endIndex)

返回此序列指定文本範圍內的統壹代碼點。

StringBuffer delete(int start, int end)

移除此序列的子字符串中的字符。

StringBuffer deleteCharAt(int index)

移除此序列指定位置的 char。

void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

將字符從此序列復制到目標字符數組 dst。

int indexOf(String str)

返回第壹次出現的指定子字符串在該字符串中的索引。

int indexOf(String str, int fromIndex)

從指定的索引處開始,返回第壹次出現的指定子字符串在該字符串中的索引。

StringBuffer insert(int offset, boolean b)

將 boolean 參數的字符串表示形式插入此序列中。

StringBuffer insert(int offset, char c)

將 char 參數的字符串表示形式插入此序列中。

StringBuffer insert(int offset, char[] str)

將 char 數組參數的字符串表示形式插入此序列中。

StringBuffer insert(int index, char[] str, int offset, int len)

將數組參數 str 的子數組的字符串表示形式插入此序列中。

StringBuffer insert(int dstOffset, CharSequence s, int start, int end)

將指定 CharSequence 的子序列插入此序列中。

StringBuffer insert(int offset, double d)

將 double 參數的字符串表示形式插入此序列中。

StringBuffer insert(int offset, float f)

將 float 參數的字符串表示形式插入此序列中。

StringBuffer insert(int offset, int i)

將 int 參數的字符串表示形式插入此序列中。

StringBuffer insert(int offset, long l)

將 long 參數的字符串表示形式插入此序列中。

StringBuffer insert(int offset, Object obj)

將 Object 參數的字符串表示形式插入此字符序列中。

StringBuffer insert(int offset, String str)

將字符串插入此字符序列中。

int lastIndexOf(String str)

返回最右邊出現的指定子字符串在此字符串中的索引。

int length()

返回長度(字符數)。

StringBuffer replace(int start, int end, String str)

使用給定 String 中的字符替換此序列的子字符串中的字符。

StringBuffer reverse()

將此字符序列用其反轉形式取代。

void setCharAt(int index, char ch)

將給定索引處的字符設置為 ch。

void setLength(int newLength)

設置字符序列的長度。

String substring(int start)

返回壹個新的 String,它包含此字符序列當前所包含的字符子序列。

String substring(int start, int end)

返回壹個新的 String,它包含此序列當前所包含的字符子序列。

String toString()

返回此序列中數據的字符串表示形式。