概念:java字符串就是Unicode字符序列;在標準的Java類庫中提供了一個預(yù)定的類,很自然地叫做String,每個用雙引號括起來的字符串都是String類的一個實例.
String 字符串API
char—————-charAt (int index) 返回給定位置的代碼單元。除非對底層的代碼單元感興趣,否則不需要調(diào)用這個方法。int —————-codePointAt(int index) 返回從給定位位置開始或結(jié)束的代碼點
int —————-offsetByCodePoints(int startIndex,int cpCount)5.0 返回從startIndex代碼點開始,位移cpCount后的代碼點索引。
int —————-compareTo(String other) 按照字典順序,如果字符串位于ohter之前,返回一個負數(shù)。如果字符串位于other之后,返回一個正數(shù)、如果兩個字符串相等,返回0.
boolean —————-endwith(String suffix) 如果字符串以suffix結(jié)尾,返回true
boolean —————-startwith(String suffix) 如果字符串以suffix開頭,返回trueboolean —————-equals(object other) 如果字符串與other相等。返回true
boolean —————-equalsIgnoreCase(object other) 如果字符串與other相等。(不區(qū)分大小寫)返回true
int —————-indexOf(String str)int —————-indexOf(String str,int fromIndex)int —————-indexOf(int cp)int —————-indexOf(int cp,int fromIndex) 返回與字符串str或者代碼點cp匹配的第一個字符串的開始位置。這個位置從索引0或者fromIndex開始計算。如果在原始串中不存在str返回-1.int —————-lastIndexOf(String str)int —————-lastIndexOf(String str,int fromIndex)int —————-lastIndexOf(int cp)int —————-lastIndexOf(int cp,int fromIndex) 返回與字符串str或代碼點cp匹配的最后一個子串的開始位置。這個位置從原始的串尾端或fromIndex開始計算。
int—————- length() 返回字符串的長度。
int —————-codePointCount(int startIndex ,int endIndex) 返回startsIndex和endIndex-1之間的代碼點數(shù)量。沒有配成對的代用字符將計入代碼點。
String ——–replace(charSequence oldString ,charSquence newString) 返回一個新的字符串,這個字符串用newString代替原始字符串中所有的oldString。可以用String或StringBuilder對象作為charSequence參數(shù)
boolean —————-startsWith(String PRefix) 如果字符串以prefix字符串開始,返回true
String —————-subString(int beginIndex)String —————-subString(int beginIndex,int endIndex) 返回一個新的字符串,這個字符串包含原始字符串中從beginIndex到串尾endIndex-1的所有代碼單元。
String —————- toLowerCase() 返回一個新的字符串,這個字符串將原始字符串的所有大寫字母改變成小寫字母
String —————-toUpperCase() 返回一個新的字符串,這個字符串將原始的字符串所有小寫字母改變成大寫字母String —————-trim() 返回一個新的字符串。這個字符串將刪除了原始字符串頭和尾部的空格空串與Null串 空串“”是長度為0的字符串;空串是一個java對象,有自己的長度(0)和內(nèi)容(空)。不過,String變量換可以放一個特殊的值,名為null,這表示目前沒有任何對象與該變量有關(guān)聯(lián); 有時要檢查一個字符串既不是null也不是空串,這種情況下就需要使用以下條件: if(str !=null && str.length() !=0) 首先要檢查str不為null,如果在一個null值上調(diào)用方法,會出現(xiàn)錯誤。
新聞熱點
疑難解答