1. 程式人生 > >JavaSE——三個特殊的類(2)

JavaSE——三個特殊的類(2)

String類(下)

1.字串查詢

從一個完整的字串之中可以判斷指定內容是否存在,查詢方法如下:

No. 方法名稱 型別 描述
1. public boolean contains(CharSequence s) 普通 判斷一個子字串是否存在
2. public int indexOf(String str) 普通 從頭開始查詢指定字串位置,查到了返回開始位置索引,如果查不到返回-1
3. public int indexOf(String str,int fromIndex) 普通 從指定位置開始查詢指定字串位置
4. public int lastIndexOf(String str) 普通 由後向前查詢子字串位置
5. public int lastIndexOf(String str,int fromIndex) 普通 從指定位置向後查詢指定字串出現位置
6. public boolean startsWith(String prefix) 普通 判斷是否以指定字串開頭
7. public boolean startsWith(String prefix),int toffset) 普通 從指定位置開始判斷是否以指定字串開頭
8. public boolean endsWith(String suffix) 普通 判斷是否以指定字串結尾

字串查詢最方便的是contains(),它是JDL1.5之後的新特性,在此前要實現必須藉助indexOf()方法實現(常用)

String str = "helloworld";
System.out.println(str.contains("world"));//true

使用indexOf()方法進行位置

String str = "helloworld";
System.out.println(str.indexOf("world"));//true
System.out.println(str.indexOf("lili"));//-1
if(str.indexOf("hello") != -1){
    System.out.println("可以查到指定字串");//可以查到指定字串
}

//注:使用indexOf()需要注意的是,如果出現內容重複它只返回查詢到的第一個位置
String str = "helloworld";
System.out.println(str.indexOf("l"));//2
System.out.println(str.indexOf("l",5));//8
System.out.println(str.lastIndexOf("l"));//8

在查詢字串時往往會判斷開頭或結尾

String str = "**@@helloworld!!";
System.out.println(str.startsWith("**"));//true
System.out.println(str.startsWith("@@"));//false
System.out.println(str.startsWith("@@",2));//true
System.out.println(str.endsWith("!!"));//true
System.out.println(str.endsWith("d"));//false

2.字串替換

使用一個新的字串替換掉已有的字串資料,可用的方法如下:

No. 方法名稱 型別 描述
1. public String replaceAll(String regex,String replacement) 普通 替換所有指定內容
2. public String replaceFirst(String regex,String replacement) 普通 替換首個指定內容

字串替換處理還與正則表示式有關,這裡先不細講。

//字串的替換處理
String str = "helloworld";
System.out.println(str.replaceAll("l","-"));//he--owor-d
System.out.println(str.replaceFirst("l","-"));// he-loworld

3.字串拆分

將一個完整的字串按照指定的分隔符劃分為若干個子字串,方法如下:

No. 方法名稱 型別 描述
1. public String[] split(String regex) 普通 將字串全部拆分
2. public String[] split(String regex,int limit) 普通 將字串部分拆分,該陣列長度就是limit極限

實現字串拆分處理

String str = "hello world hello bit";
String[] result = str.split(" ");//按照空格拆分
for(String s : result){
    System.out.println(s);
}
//輸出
//hello
//world
//hello
//bit

將字串的部分拆分

String str = "hello world hello bit";
String[] result = str.split(" ",2);
for(String s : result){
    System.out.println(s);
}
//輸出:
//hello
//world hello bit

有些內容無法拆分就需要使用""轉義

//eg:拆分IP地址
String str = "192.168.1.1";
String[] result = str.split("\\.");
for(String s : result){
    System.out.println(s);
}
//輸出
//191
//168
//1
//1

比較常用的是多次拆分

String str = "zoujier:18|zoudapao:81";
String[] result = str.split("\\|");
for(int i = 0; i < result.length; i ++){
    String[] tmp = result[i].split(":");
    System.out.println(temp[0] + "=" +temp[1]);
}
//輸出
//zoujier=18
//zoudapao=81

4.字串擷取

從一個完整的字串中取出部分內容

No. 方法名稱 型別 描述
1. public String substring(int beginIndex) 普通 從指定索引擷取到結尾
2. public String substring(int beginIndex,int endIndex) 普通 擷取部分內容

觀察字串擷取

String str = "helloworld";
System.out.println(str.substring(5));//world
System.out.println(str.substring(3,5));//lo

注:第二種擷取方式從beginIndex索引開始擷取,beginIndex對應的位置要擷取上,到endIndex結束,endIndex對應的位置不擷取。

5.字串其他操作方法

No. 方法名稱 型別 描述
1. public String trim() 普通 去掉字串中的左右空格,保留之間空格
2. public String toUpperCase() 普通 字串轉大寫
3. public String toLowerCase() 普通 字串轉小寫
4. public native String intern() 普通 字串入池操作
5. public String concat(String str) 普通 字串拼接,等同於"+",不入池
6. public int length() 普通 取得字串長度
7. public boolean isEmpty() 普通 判斷是否為空字串,但不是null,而是長度0

trim()方法

String str = "    hello  world";
System.out.println("[" + str + "]");//[    hello  world]
System.out.println("[" + str.trim() + "]");//[hello  world]

大小寫轉換(這兩個方法只轉換字母)

String str = "   hello%$$&%%&*WORLD  zoujierchibaba ";
System.out.println(str.toUpperCase());//   HELLO%$$&%%&*WORLD  ZOUJIERCHIBABA 
System.out.println(str.toLowerCase());//   hello%$$&%%&*world  zoujierchibaba 

陣列長度使用陣列名稱.length是屬性,而String中的length()是方法

String str = "hello" ;
System.out.println(str.length());//5

isEmpty()方法

System.out.println("hello".isEmpty());//false
System.out.println("".isEmpty());//true
System.out.println(new String().isEmpty());//true

6.首字母大寫操作

String類中沒有提供首字母大寫操作,需要自己實現

public static void main(String[] args) {
   //首字母大寫
   System.out.println(fistUpper("zoujier"));//Zoujier
   System.out.println(fistUpper(""));//
   System.out.println(fistUpper("z"));//Z
}
public static String fistUpper(String str) {
   //字串為null或者長度為0時
   if ("".equals(str)||str==null) {
      return str ;
   }
   //字串長度大於1時
   if (str.length()>1) {
      return str.substring(0, 1).toUpperCase()+str.substring(1) ;//取出第一個字元轉大寫再拼接第一個字母之後的內容
   }
   //字串只要一個字元時
   return str.toUpperCase() ;
}