String类常用方法练习


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 }

运行:


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM