天天看點

String類

常見構造方法

• public string():空構造

• public string(byte[] bytes):把位元組數組轉成字元串

• public string(byte[] bytes,int index,int length):把位元組數組的一部分轉成字元串

• public string(char[] value):把字元數組轉成字元串

• public string(char[] value,int index,int count):把字元數組的一部分轉成字元串

• public string(string original):把字元串常量值轉成字元串

string類的判斷功能

• boolean equals(object obj):比較字元串的内容是否相同,區分大小寫

• boolean equalsignorecase(string str):比較字元串的内容是否相同,忽略大小寫

• boolean contains(string str):判斷大字元串中是否包含小字元串

• boolean startswith(string str):判斷字元串是否以某個指定的字元串開頭

• boolean endswith(string str):判斷字元串是否以某個指定的字元串結尾

• boolean isempty():判斷字元串是否為空。

string類的擷取功能 //不存在則傳回-1

• int length():擷取字元串的長度。

• char charat(int index):擷取指定索引位置的字元

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

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

• int indexof(int ch,int fromindex):傳回指定字元在此字元串中從指定位置後第一次出現處的索引。

• int indexof(string str,int fromindex):傳回指定字元串在此字元串中從指定位置後第一次出現處的索引。

• lastindexof

//包含頭,不包含尾,左閉右開,substring會産生一個新額字元串,需要将新的字元串記錄

• string substring(int start):從指定位置開始截取字元串,預設到末尾。

• string substring(int start,int end):從指定位置開始到指定位置結束截取字元串。

string類的切割功能:public string[] split(string regex)

string類的替換功能:public string replaceall(string regex,string replacement)

string類的轉換功能

• byte[] getbytes():把字元串轉換為位元組數組。

gbk碼表一個中文代表兩個位元組,gbk碼表特點,中文的第一個位元組肯定是負數

• char[] tochararray():把字元串轉換為字元數組。

• static string valueof(char[] chs):把字元數組轉成字元串。底層是由string類的構造方法完成的

• static string valueof(int i):把int類型的資料轉成字元串。

• 注意:string類的valueof方法可以把任意類型的資料轉成字元串。

• string tolowercase():把字元串轉成小寫。(了解)

• string touppercase():把字元串轉成大寫。

• string concat(string str):把字元串拼接。

用+拼接字元串更強大,可以用字元串與任意類型相加,concat方法調用的和傳入的都必須是字元串

string的替換功能 //不存在,保留原字元不改變

• string replace(char old,char new) //替換了裡面所有old字元

• string replace(string old,string new)

string的去除字元串兩空格功能

• string trim() //去除兩端的空格

string的按字典順序比較兩個字元串

• int compareto(string str)

• int comparetoignorecase(string str)(了解)