1、提取子串
用String類的substring方法可以提取字符串中的子串,該方法有兩種常用參數(shù):
public String substring(int beginIndex)//該方法從beginIndex位置起,從當前字符串中取出剩余的字符作為一個新的字符串返回。
public String substring(int beginIndex, int endIndex)//該方法從beginIndex位置起,從當前字符串中取出到endIndex-1位置的字符作為一個新的字符串返回。
2、字符串比較
public int compareTo(String anotherString)//該方法是對字符串內(nèi)容按字典順序進行大小比較,通過返回的整數(shù)值指明當前字符串與參數(shù)字符串的大小關(guān)系。若當前對象比參數(shù)大則返回正整數(shù),反之返回負整數(shù),相等返回0。
public int compareToIgnoreCase(String anotherString)//與compareTo方法相似,但忽略大小寫。
public boolean equals(Object anotherObject)//比較當前字符串和參數(shù)字符串,在兩個字符串相等的時候返回true,否則返回false。
public boolean equalsIgnoreCase(String anotherString)//與equals方法相似,但忽略大小寫。
3、字符串中單個字符查找
public int indexOf(int ch/String str)//用于查找當前字符串中字符或子串,返回字符或子串在當前字符串中從左邊起首次出現(xiàn)的位置,若沒有出現(xiàn)則返回-1。
public int indexOf(int ch/String str, int fromIndex)//改方法與第一種類似,區(qū)別在于該方法從fromIndex位置向后查找。
public int lastIndexOf(int ch/String str)//該方法與第一種類似,區(qū)別在于該方法從字符串的末尾位置向前查找。
public int lastIndexOf(int ch/String str, int fromIndex)//該方法與第二種方法類似,區(qū)別于該方法從fromIndex位置向前查找。
4、大小寫轉(zhuǎn)換
public String toLowerCase()//返回將當前字符串中所有字符轉(zhuǎn)換成小寫后的新串
public String toUpperCase()//返回將當前字符串中所有字符轉(zhuǎn)換成大寫后的新串
5、字符串中字符的替換
public String replace(char oldChar, char newChar)//用字符newChar替換當前字符串中所有的oldChar字符,并返回一個新的字符串。
public String replaceFirst(String regex, String replacement)//該方法用字符replacement的內(nèi)容替換當前字符串中遇到的第一個和字符串regex相匹配的子串,應將新的字符串返回。
public String replaceAll(String regex, String replacement)//該方法用字符replacement的內(nèi)容替換當前字符串中遇到的所有和字符串regex相匹配的子串,應將新的字符串返回。
本文由培訓無憂網(wǎng)長沙牛耳教育專屬課程顧問老師整理發(fā)布,更多相關(guān)課程請關(guān)注培訓無憂網(wǎng)Java培訓或添加老師微信:15033336050
以上文章由長沙牛耳教育課程顧問整理編輯發(fā)布,部分文章來自網(wǎng)絡內(nèi)容真實性請自行核實或聯(lián)系我們,了解相關(guān)專業(yè)課程信息您可在線咨詢也可免費申請試課。關(guān)注官方微信了解更多:150 3333 6050
免 費 申 請 試 課