String類的常用操作方法

2021-06-23 03:55:57 字數 2576 閱讀 7140

· 字元與字串:

|- 【構造方法】將全部的字元陣列變為字串:

public string(char  data)

;|- 【構造方法】將部分字元陣列變為字串:

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

;|- 〖普通方法〗將字串變為字元陣列:

public char  tochararray()

;|- 〖普通方法〗取得指定索引位置上的字元:

public char charat(int index)

· 位元組與字串:

|- 【構造方法】將全部的位元組陣列變為字串:

public string(byte  data)

;|- 【構造方法】將部分的位元組陣列變為字串:

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

;|- 〖普通方法〗將字串變為位元組陣列:

public byte  getbytes()

;|- 〖普通方法〗編碼轉換:

public byte  getbytes(string charset) throws unsupportedencodingexception

· 字串查詢:

|- 〖普通方法〗判斷是否有指定的子字串:

public boolean contains(string str)

;|- 〖普通方法〗從頭查詢指定字串的位置:

public int indexof(string str)

;|- 〖普通方法〗從指定索引查詢字串的位置:

public int indexof(string str,int offset)

;|- 〖普通方法〗從後向前查詢字串的位置:

public int lastindexof(string str)

;|- 〖普通方法〗從指定位置索引由後向前查詢:

public int lastindexof(string str , int offset)

;|- 〖普通方法〗判斷是否以指定的字串開頭:

public boolean startswith(string str)

;|- 〖普通方法〗從指定索引判斷是否以指定的字串開頭:

public boolean startswith(string str, int offset)

;|- 〖普通方法〗判斷是否以指定的字串結尾:

public boolean endswith(string str)

· 字串關係判斷:

|- 〖普通方法〗區分大小寫比較:

public boolean equals(string str)

;|- 〖普通方法〗不區分大小寫比較:

public boolean equalsignorecase(string str)

;|- 〖普通方法〗判斷大小關係:

public int compareto(string str)

· 字串替換:

|- 〖普通方法〗全部替換:

public string replaceall(string regex,string str)

;|- 〖普通方法〗替換首個:

public string replacefirst(string regex,string str)

· 字串擷取:

|- 〖普通方法〗從指定位置擷取到結尾:

public string substring(int beginindex)

;|- 〖普通方法〗擷取部分字串:

public string substring(int beginindex, int endindex)

· 字串拆分:

|- 〖普通方法〗全拆分:

public string  split(string regex)

;|- 〖普通方法〗部分拆分:

public string  split(string regex,int limit)

· 其他方法:

|- 〖普通方法〗轉大寫:

public string touppercase()

;|- 〖普通方法〗轉小寫:

public string tolowercase()

;|- 〖普通方法〗取得字串的長度:

public int length()

;|- 〖普通方法〗去掉左右空格:

public string trim()

;|- 〖普通方法〗判斷空字串:

public boolean isempty()

;|- 〖普通方法〗入池:

public boolean intern()

;|- 〖普通方法〗字串連線:

public string concat(string str)

String類的常用操作方法列舉

public static void main string args 1.將字元陣列中的所有內容變為字串 string str new string data system.out.println str 2.將部分字元陣列中的內容變為字串 string str2 new string data,...

String的常用操作方法

在string類中提供了以下的方法操作字元與字串間的轉換關係 根據字串中提供的索引找到指定位置的字元 public charcharat int index 將字串變為字元陣列 public chartochararray 將字元陣列變為字串 將全部的字元陣列變為string型別 publicstr...

String類 常用 操作 方法 合集 慢慢完善

string用的挺多,但是針對它的操作方法時間長了不用還真想不起來,總結一下子的話,這裡先總結下常用的 返回值型別 基本操作方法 機能boolean equals string str 和其他字串相等時返回true char charat int pos 獲取字串中的第i個字元 intindexof...