Java 字串常用操作(String類)
字串查詢
indexOf(String s)
該方法用於返回引數字串s在指定字串中首次出現的索引位置,當呼叫字串的indexOf()方法時,會從當前字串的開始位置搜尋s的位置;如果沒有檢索到字串s,該方法返回-1String str ="We are students"; int size = str.indexOf("a"); // 變數size的值是3
- lastIndexOf(String str)
該方法用於返回字串最後一次出現的索引位置。當呼叫字串的lastIndexOf()方法時,會從當前字串的開始位置檢索引數字串str,並將最後一次出現str的索引位置返回。如果沒有檢索到字串str,該方法返回-1.
如果lastIndexOf方法中的引數是空字串”” ,則返回的結果與length方法的返回結果相同。
獲取指定索引位置的字元
使用charAt()方法可將指定索引處的字元返回。
String str = "hello word";
char mychar = str.charAt(5); // mychar的結果是w
獲取子字串
substring(int beginIndex)
該方法返回的是從指定的索引位置開始擷取知道該字串結尾的子串。String str = "Hello word"; String substr = str.substring(3); //獲取字串,此時substr值為lo word
- substring(int beginIndex, int endIndex)
beginIndex : 開始擷取子字串的索引位置
endIndex:子字串在整個字串中的結束位置
String str = "Hello word";
String substr = str.substring(0,3); //substr的值為hel
去除空格
trim()方法返回字串的副本,忽略前導空格和尾部空格。
字串替換
replace(oldchar,newChar)方法可實現將指定的字元或字串替換成新的字元或字串
oldChar:要替換的字元或字串
newChar:用於替換原來字串的內容
如果要替換的字元oldChar在字串中重複出現多次,replace()方法會將所有oldChar全部替換成newChar。需要注意的是,要替換的字元oldChar的大小寫要與原字串中字元的大小寫保持一致。
String str= "address";
String newstr = str.replace("a", "A");// newstr的值為Address
判斷字串的開始與結尾
startsWith()方法與endsWith()方法分別用於判斷字串是否以指定的內容開始或結束。這兩個方法的返回值都為boolean型別。
1. startsWith(String prefix)
該方法用於判斷當前字串物件的字首是否是引數指定的字串。
2. endsWith(String suffix)
該方法用於判斷當前字串是否以給定的子字串結束
判斷字串是否相等
- equals(String otherstr)
如果兩個字串具有相同的字元和長度,則使用equals()方法比較時,返回true。同時equals()方法比較時區分大小寫。 - equalsIgnoreCase(String otherstr)
equalsIgnoreCase()方法與equals()型別,不過在比較時忽略了大小寫。
按字典順序比較兩個字串
compareTo()方法為按字典順序比較兩個字串,該比較基於字串中各個字元的Unicode值,按字典順序將此String物件表示的字元序列與引數字串所表示的字元序列進行比較。如果按字典順序此String物件位於引數字串之前,則比較結果為一個負整數;如果按字典順序此String物件位於引數字串之後,則比較結果為一個正整數;如果這兩個字串相等,則結果為0.
str.compareTo(String otherstr);
字母大小寫轉換
字串的toLowerCase()方法可將字串中的所有字元從大寫字母改寫為小寫字母,而tuUpperCase()方法可將字串中的小寫字母改寫為大寫字母。
str.toLowerCase();
str.toUpperCase();
字串分割
使用split()方法可以使字串按指定的分隔字元或字串對內容進行分割,並將分割後的結果存放在字元陣列中。
str.split(String sign);
sign為分割字串的分割符,也可以使用正則表示式。
沒有統一的對字串進行分割的符號,如果想定義多個分割符,可使用符號“|”。例如,“,|=”表示分割符分別為“,”和“=”。
str.split(String sign, in limit);
該方法可根據給定的分割符對字串進行拆分,並限定拆分的次數
獲取字串的長度 length()
把字串轉化為相應的數值
- int型Integer.parseInt(字串)
- long型Long.parseLong(字串)
- double型Double.valueOf(字串).doubleValue()
字串陣列與字串之間的轉換
一個字串可以變為一個字元 陣列,同樣,也可以把一個字元陣列,變為一個字串。
在String類中提供了以下操作方法:
將字串變為字元陣列:
public char[] toCharArray()
public String(char[] value)
public String(char[] value,int offset,int count)
public class StringAPIDemo01{
public static void main(String args[]){
String str1 = "hello" ; // 定義字串
char c[] = str1.toCharArray() ; // 將一個字串變為字元陣列
for(int i=0;i<c.length;i++){ // 迴圈輸出
System.out.print(c[i] + "、") ;
}
System.out.println("") ; // 換行
String str2 = new String(c) ; // 將全部的字元陣列變為String
String str3 = new String(c,0,3) ; // 將部分字元陣列變為String
System.out.println(str2) ; // 輸出字串
System.out.println(str3) ; // 輸出字串
}
};
字串與byte陣列的轉換
byte陣列(位元組陣列),在一般的IO操作中會經常使用到。
在String類中提供了以下的方法可以進行字串與位元組陣列間的轉換:
字串變為位元組陣列:public byte[] getBytes()
將一個位元組陣列變為字串:
將全部位元組陣列變為String:pbulic String(byte[] bytes)
將部分位元組陣列變為String:public String(byte[] bytes,int offset,int length)
public class StringAPIDemo03{
public static void main(String args[]){
String str1 = "hello" ; // 定義字串
byte b[] = str1.getBytes() ; // 將字串變為byte陣列
System.out.println(new String(b)) ; // 將全部的byte陣列變為字串
System.out.println(new String(b,1,3)) ; // 將部分的byte陣列變為字串
}
};