String
類代表字符串。Java 程序中的所有字符串字面值(如 "abc"
)都作為此類的實例實現。
字符串是常量;它們的值在創建之后不能更改。字符串緩沖區支持可變的字符串。
String
類包括的方法可用於檢查序列的單個字符、比較字符串、搜索字符串、提取子字符串、
創建字符串副本並將所有字符全部轉換為大寫或小寫。
String類是最常用的類之一,下面就通過幾個練習,熟悉String類中的提供的重要方法。
字符串練習一
給定一個字符串數組,按照字典順序,進行大小寫排序
思路:
1.對數組排序,可以用選擇排序、冒泡排序等等。
2.for循環嵌套,比較,交換位置。
3.不同之處,以前比較的是數字,用的是比較運算符;
現在比較的是字符串對象,應該使用compareTo方法。
1 public class StringTest_1 2 { 3 //對字符串數組進行排序
4 public static void stringSort(String[] arr) 5 { 6 //采用冒泡排序
7 for(int i=0;i<arr.length-1;i++) 8 { 9 for(int j=0;j<arr.length-1-i;j++) 10 { 11 //用compareTo方法進行字符串比較
12 if(arr[j].compareTo(arr[j+1])>0) 13 { 14 String temp=arr[j]; 15 arr[j]=arr[j+1]; 16 arr[j+1]=temp; 17 } 18 } 19 } 20 showArray(arr); 21 } 22
23 //定義方法,以[str1,str2,str3]的格式來打印數組
24 public static void showArray(String[] arr) 25 { 26 System.out.print("["); 27 for(int i=0;i<arr.length;i++) 28 { 29 if(i!=arr.length-1) 30 System.out.print(arr[i]+","); 31 else
32 { 33 System.out.print(arr[i]+"]\n"); 34 } 35 } 36 } 37 public static void main(String[] args) 38 { 39 String arr[]={"nba","abc","cba","zz","qq","haha"}; 40 //打印數組
41 showArray(arr); 42 //對數組進行排序並輸出
43 stringSort(arr); 44 } 45 }
運行:
字符串練習二
一個子串在字符串中出現的次數
思路:
1.用indexOf方法獲取子串在字符串中第一次出現的位置index
2.再用indexOf方法獲取以(index+子串長度)為起始的剩下的字符串中子串出現的位置,直到字符串中不再包含子串。
可用while循環實現。
3.每次找到后用計數器記錄即可。
1 public class StringTest_2 2 { 3 public static void main(String[] args) 4 { 5 String str="abcqwabcedcxabcuabcjkabcnmbabc"; 6 //String str=null;
7 try
8 { 9 int count=countChildStr(str,"abc"); 10 System.out.println("abc在"+str+"中出現的次數為:"+count); 11 } 12 catch (NullPointerException ne) 13 { 14 System.out.println(ne); 15 } 16 catch(RuntimeException re) 17 { 18 System.out.println(re); 19 } 20 } 21 public static int countChildStr(String str,String key) 22 { 23 if(str==null||key==null) 24 { 25 throw new NullPointerException("空指針異常,源字符串和子串都不能為NULL"); 26 } 27 if(key=="") 28 {throw new RuntimeException("調用不合法,子串要有內容");} 29 int count=0,index=0; 30 while((index=str.indexOf(key,index))!=-1) 31 { 32 count++; 33 index+=key.length(); 34 } 35 return count; 36 } 37 }
字符串練習三
找到兩個字符串的最大公共子串
思路:
1.判斷較長字符串中是否包含較短字符串,如果包含,則較短字符串則為最大公共子串。
2.如果不包含,就對較短字符串以長度遞減的方式取子串,去較長字符串中判斷是否包含,
如果包含就找到了,不用再找了。
3.重點:對字符串以長度遞減的方式取子串
1 public class StringTest_3 2 { 3 public static void main(String[] args) 4 { 5 //創建兩個不為空的字符串
6 String str1="abxczwsxcvdfas"; 7 //String str1=null;
8 String str2="ghwsxcvxcdbgthnnnrfqwe"; 9
10 try
11 { 12 String str=searchMaxCommonStr(str1,str2); 13 System.out.println("最大公共子串是:"+str); 14 } 15 catch (NullPointerException ne) 16 { 17 System.out.println(ne); 18 } 19 } 20 public static String searchMaxCommonStr(String str1,String str2) 21 { 22 if(str1==null||str2==null) 23 throw new NullPointerException("空指針異常,參數不能為Null"); 24 //斷定較長字符串和較短字符串
25 String max=(str1.length()>str2.length())?str1:str2; 26 String min=(str1.equals(max))?str2:str1; 27
28 //按長度遞減的方式取子串,從min.length~~1
29 for(int i=min.length();i>0;i--) 30 { 31 for(int x=0,y=x+i;y<min.length();x++,y++) 32 { 33 String childStr=min.substring(x,y); 34 //若較長字符串中包含此子串,則找到了 35 //否則繼續找
36 if(max.contains(childStr)) 37 return childStr; 38 } 39 } 40 return null; 41 } 42 }
運行:
字符串練習四
寫一個和trim功能相同的方法
思路:
1.定義兩個變量,用來存儲兩個角標
2.分別從頭和尾遍歷字符串,直到找到第一個不為空格的字符
3.截取字符串
1 public class StringTest_4 2 { 3 public static void main(String[] args) 4 { 5 String str=" abc ws "; 6 str=myTrim(str); 7 System.out.println(str); 8
9 } 10 public static String myTrim(String s) 11 { 12 int begin=0,end=s.length()-1; 13 //從頭遍歷
14 while(begin<=end && s.charAt(begin)==' ') 15 { 16 begin++; 17 } 18 //從尾部遍歷
19 while(begin<=end && s.charAt(end)==' ') 20 { 21 end--; 22 } 23 return s.substring(begin,end+1); 24 } 25 }
運行: