String類常用方法

2021-09-26 03:18:17 字數 3576 閱讀 5334

string:不可變長的字串行(在做字串拼接的時候會直接在堆中建立乙個新的字串物件,並使用這個字串物件儲存新的值,指標直接指向新的字串物件的位址。)

stringbuilder:可變長的字串行,執行緒不安全,效率較高

stringbuffer:可變長的字串行,執行緒安全,效率較低

string(byte bytes)通過使用平台的預設字符集解碼指定的 byte 陣列,構造乙個新的 string。

string(byte bytes, string charsetname)通過使用指定的 charset 解碼指定的 byte 陣列,構造乙個新的 string。

string(byte bytes, int offset, int length)通過使用平台的預設字符集解碼指定的 byte 子陣列,構造乙個新的 string。

string(byte bytes, int offset, int length, charset charset)通過使用指定的 charset 解碼指定的 byte 子陣列,構造乙個新的 string。

string()初始化乙個新建立的 string 物件,使其表示乙個空字串行。例:string str=new string(「abc」);

string(char value)分配乙個新的 string,使其表示字元陣列引數中當前包含的字串行。

string(char value, int offset, int count)分配乙個新的 string,它包含取自字元陣列引數乙個子陣列的字元。

string(int codepoints, int offset, int count)分配乙個新的 string,它包含 unicode **點陣列引數乙個子陣列的字元。

string(stringbuilder builder)分配乙個新的字串,它包含字串生成器引數中當前包含的字串行。

char charat(int index)返回指定索引處的 char 值。

int codepointat(int index)返回指定索引處的字元(unicode **點)。

int compareto(string anotherstring)按字典順序比較兩個字串。

int comparetoignorecase(string str)按字典順序比較兩個字串,不考慮大小寫。

string concat(string str)將指定字串連線到此字串的結尾。

boolean contains(charsequence s)當且僅當此字串包含指定的 char 值序列時,返回 true。

string copyvalueof(char data)返回指定陣列中表示該字串行的 string。

string copyvalueof(char data, int offset, int count)返回指定陣列中表示該字串行的 string。

boolean startswith(string prefix)測試此字串是否以指定的字首開始。

boolean startswith(string prefix, int toffset)測試此字串從指定索引開始的子字串是否以指定字首開始。

boolean endswith(string suffix)測試此字串是否以指定的字尾結束。

byte getbytes()使用平台的預設字符集將此 string 編碼為 byte 序列,並將結果儲存到乙個新的 byte 陣列中。

byte getbytes(string charsetname)使用指定的字符集將此 string 編碼為 byte 序列,並將結果儲存到乙個新的 byte 陣列中。

int indexof(int ch)返回指定字元在此字串中第一次出現處的索引。

int indexof(string str)返回指定子字串在此字串中第一次出現處的索引。

int indexof(int ch, int fromindex)返回在此字串中第一次出現指定字元處的索引,從指定的索引開始搜尋。

int lastindexof(int ch)返回指定字元在此字串中最後一次出現處的索引。

int length()返回此字串的長度。

string replace(char oldchar, char newchar)返回乙個新的字串,它是通過用 newchar 替換此字串**現的所有 oldchar 得到的。

string split(string regex)根據給定正規表示式的匹配拆分此字串。

string substring(int beginindex)返回乙個新的字串,它是此字串的乙個子字串。

string substring(int beginindex, int endindex)返回乙個新字串,它是此字串的乙個子字串。

char tochararray()將此字串轉換為乙個新的字元陣列。

string touppercase()使用預設語言環境的規則將此 string 中的所有字元都轉換為大寫。

string tolowercase()使用預設語言環境的規則將此 string 中的所有字元都轉換為小寫。

string trim()返回字串的副本,忽略前導空白和尾部空白。 (去前後空格)

static string valueof(int i)返回 int 引數的字串表示形式。 (引數可為多種型別)

stringbuffer()構造乙個其中不帶字元的字串緩衝區,初始容量為 16 個字元。

stringbuffer(string str)構造乙個字串緩衝區,並將其內容初始化為指定的字串內容。

stringbuffer(int capacity)構造乙個不帶字元,但具有指定初始容量的字串緩衝區。

int capacity()返回初始容量。

int length()返回長度(字元數)。

stringbuffer delete(int start, int end)刪除字元

stringbuffer insert(int offset, int i)將引數的字串表示形式插入到此序列中。(引數可為多種型別)

stringbuffer reverse()字串行被序列的反向替換。

String 類常用方法

字串 就是由多個字元組成的一串陣列 一旦被複製,就不能被改變 public class stringdemo string s2 new string bys system.out.println s2 s2 system.out.println s2.length s2.length 5 syst...

String類常用方法

方法名稱 型別 方法描述 public string char value 構造 將字元陣列變為string類物件 public string char value,int offset int count 構造 將部分字元陣列變為string類物件 public char charat int i...

String類常用方法

返回字串長度 public int length 返回字串中指定位置的字元 public char charat int index 提取字串 方法說明 public string substring int beginindex 從beginindex位置起,從當前字串中取出剩餘的字元作為乙個新的...