String類常用方法

2021-08-20 13:34:55 字數 4572 閱讀 6621

方法名稱

型別

方法描述

public string(char value)

構造

將字元陣列變為string類物件

public string(char value,int  offset ,int  count)

構造

將部分字元陣列變為string類物件

public char charat(int index)

普通

返回指定索引對應的字元資訊

public char tochararray()

普通

將字串以字元陣列的形式返回

public string(byte bytes)

構造

將全部位元組陣列變為字串

public string(byte bytes,int  offset,int  length)

構造

將部分位元組陣列變為字串

public byte getbytes()

普通

將字串變為位元組陣列

public byte getbytes(string charsename)throws unsupportedencodingexception

普通

進行編碼轉換

public boolean equals(object anobject)

普通

進行相等判斷,它區分大小寫

public boolean equalsignorecase(string anotherstring)

普通

進行相等判斷,它不區分大小寫

public int compareto(string anotherstring)

普通

判斷兩個字串的大小(按照字元編碼比較),若兩個字串內容相等則值=0,若大於則值大於0,若小於則值小於0

public int comparetoignorecase(string str)

普通

判斷兩個字串的大小(不區分大小寫)

public boolean contains(charsequence  s)

普通

判斷指定的內容是否存在

public int indexof(string str)

普通

從頭開始查詢指定字串的位置(由前向後),若存在則返回(第乙個字母)位置的索引值,若不存在則返回-1

public int indexof(string str,int fromindex)

普通

由指定位置從前向後查詢指定字串的位置(由前向後),若存在則返回(第乙個字母)位置的索引值,若不存在則返回-1

public int lastindexof(string str)

普通

由後向前查詢指定字串的位置,若存在則返回(第乙個字母)位置的索引值,若不存在則返回-1

public int lastindexof(string str, int fromindex)

普通

從指定位置開始由後向前查詢指定字串的位置,若存在則返回(第乙個字母)位置的索引值,若不存在則返回-1

public boolean startswith(string prefix)

普通

判斷是否以指定的字串開頭

public boolean startswith(string prefix, int toffest)

普通

從指定位置開始判斷是否以指定的字串開

public boolean endswith(string suffix)

普通判斷是否以指定的字串結尾

public boolean endswith(string suffix,int toffest)

普通從指定位置開始判斷是否以指定的字串結尾

public string replaceall(string regex,string replacement)普通用新的內容替換掉全部舊的內容

public string replacefirst(string regex,string replacement)

普通替換首個滿足條件的內容

public string substring (int beginindex)普通從指定索引位置擷取到結尾

public string substring (int beginindex, int endindex)

普通擷取部分字串(不包括結尾索引值)

public string split(string regex)普通將指定的字串進行全部拆分

public string split(string regex, int limit)

普通將指定的字串進行部分拆分,limit就是最終陣列長度的值(如果陣列可拆的話)

public string concat(string str)普通字串的連線,類似於「+」

public string tolowercase()普通全部字母轉小寫(非字母不會)

public string touppercase()普通全部字母轉大寫(非字母不會)

public string trim()普通去掉字串中兩邊的空格(中間的不去掉)

public int length()普通取得字串的長度

public string intern()普通資料入池

public boolean isempty()普通判斷字串是否為空(空不是null,而是"",而" "不是空!)

and  未完待續.........

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

String類常用方法

string 不可變長的字串行 在做字串拼接的時候會直接在堆中建立乙個新的字串物件,並使用這個字串物件儲存新的值,指標直接指向新的字串物件的位址。stringbuilder 可變長的字串行,執行緒不安全,效率較高 stringbuffer 可變長的字串行,執行緒安全,效率較低 string byte...