String—Test

 

[html]  view plain copy
  1. /*  
  2. 1.模拟一个trim方法,去除字符串两端的空格。  
  3.   思路:  
  4.       1,判断字符串第一个位置是否是空格,如果是继续向下  
  5.          判断,知道不是空格为止,结尾处判断空格也是如此。  
  6.       2,当开始和结尾都判断到不是空格时,就是要获取的字  
  7.          符串。    
  8.     
  9. 2.将一个字符串进行反转,将字符串中指定部分进行反转,"abcdefg","abfedcg"  
  10.     思路:  
  11.         1,曾经学习过对西方的元素进行反转。  
  12.         2,将字符串变成数组,对数组反转。  
  13.         3,将反转后的数组变成字符串。  
  14.         4,只要将要反转的部分的开始和结束为止作为参数传递即可。  
  15.           
  16. 3.获取一个字符串在另一个字符串中出现的次数。  
  17.     "abkkcdkkefkkskk"  
  18.     思路:  
  19.         1,定义一个计数器。  
  20.         2,获取kk第一次出现的位置。  
  21.         3,从第一次出现位置后剩余的字符串中继续获取kk 出现的位置。  
  22.            每获取一次就计数一次。  
  23.         4,当获取不到时,计数完成。  
  24.       
  25. 4.获取两个字符串中最大相同子串。第一个动作:将短的那个串进行  
  26.   长度依次递减子串打印。  
  27.   "abcwerthelloyuiodef"  
  28.   "cvhellobnm"  
  29.   思路:  
  30.       1,将短的那个子串按照长度递减的方式获取到。  
  31.       2,将每获取到的子串去长串中判断是否包含。  
  32.          如果包含,已经找到。  
  33. */  
  34. class StringTest  
  35. {  
  36.     public static void sop(String str)  
  37.     {  
  38.         System.out.println(str);      
  39.     }     
  40.     public static void main(String[] args)  
  41.     {  
  42.         String s1 = "awerthelloyuiodef";  
  43.         String s2 = "acvhellobnm";  
  44.         sop(getMaxSubString(s1,s2));  
  45. //      String str = "abkkcdkkefkkskk";  
  46. //      sop("count="+getSubCount2(str,"kk"));  
  47. //      String s = "    bc affg     ";  
  48. //      sop(s);  
  49. //      s = myTrim(s);  
  50. //      sop(s);  
  51.           
  52. //      sop(reverseString(s));  
  53. //      sop(reverseString(s,4,8));  
  54.     }  
  55.       
  56.     //练习四  
  57.     public static String getMaxSubString(String s1, String s2)  
  58.     {  
  59.         String max = ""min = "";  
  60.         max = (s1.length()>s1.length())?s1:s2;  
  61.         min = (max == s1)?s2:s1;  
  62.           
  63.         for(int x=0; x<min.length(); x++)  
  64.         {  
  65.             for(int y=0z=min.length()-x; z!=min.length()+1; y++,z++)  
  66.             {  
  67.                 String temp = min.substring(y,z);  
  68. //              sop(temp);  
  69.                   
  70.                 if(max.contains(temp))//if(s1.indexOf(temp)!=-1)  
  71.                 return temp;  
  72.   
  73.             }  
  74.         }     
  75.         return "";  
  76.     }  
  77.       
  78.     //练习三,方式二  
  79.     public static int getSubCount2(String str, String key)  
  80.     {  
  81.         int count = 0;  
  82.         int index = 0;  
  83.           
  84.         while((index=str.indexOf(key,index)) != -1)  
  85.         {  
  86.                 sop("index="+index);  
  87.                 index = index+key.length();  
  88.                   
  89.                 count++;  
  90.         }  
  91.         return count;  
  92.     }  
  93.     //练习三  
  94.     public static int getSubCount(String str, String key)  
  95.     {  
  96.         int count = 0;   
  97.         int index = 0;  
  98.           
  99.         while((index=str.indexOf(key)) != -1)  
  100.         {  
  101.             sop("str="+str);  
  102.             str = str.substring(index+key.length());  
  103.                   
  104.             count++;  
  105.         }  
  106.         return count;  
  107.               
  108.     }  
  109.       
  110.     //练习二:扩展--->  
  111.     public static String reverseString(String s, int start, int end)  
  112.     {  
  113.         //字符串变成数组  
  114.         char[] chs = s.toCharArray();  
  115.           
  116.         //反转数组  
  117.         reverse(chs,start,end);  
  118.           
  119.         //数组变成字符串  
  120.         return new String(chs);   
  121.     }  
  122.         private static void reverse(char[] arr, int x, int y)  
  123.     {  
  124.         for(int start=xend=y-1 ; start<end; start++,end--)  
  125.         {  
  126.             swap(arr,start,end);      
  127.         }     
  128.     }  
  129.       
  130.     //练习二:将字符串反转  
  131.     public static String reverseString(String s)  
  132.     {  
  133.         return reverseString(s,0,s.length());  
  134.     }  
  135.     private static void reverse(char[] arr)  
  136.     {  
  137.         for(int start=0,end=arr.length-1; start<end; start++,end--)  
  138.         {  
  139.             swap(arr,start,end);      
  140.         }     
  141.     }  
  142.     private static void swap(char[] arr, int x, int y)  
  143.     {  
  144.             char temp = arr[x];  
  145.             arr[x] = arr[y];  
  146.             arr[y] = temp;  
  147.     }  
  148.       
  149.     //练习一:去除字符串两端空格  
  150.     public static String myTrim(String str)  
  151.     {  
  152.         int start=0end=str.length()-1;  
  153.           
  154.         while(start<=end && str.charAt(start) == ' ')  
  155.             start++;  
  156.               
  157.         while(start<=end && str.charAt(end) == ' ')  
  158.             end--;  
  159.               
  160.         return str.substring(start,end+1);  
  161.               
  162.     }  
  163.       
  164. }  


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值