第七周技术博客

 /**
this is a class to deal with String, its provided with Trim, reverse etc
@author Lu Shan
@version v1.8

 */

 public class StrTool
 {
     /**
    trim: to remove the spaces on the two sides of the String
    @param temp to get a String
    @return return the new String
     */
     public static String myTrim(String temp)
     {
         int i=0;
         while(temp.charAt(i)==' ')
         {
             i++;
         }
         int start=i;
         i=temp.length()-1;
         while(temp.charAt(i)==' ')
         {
             i--;
         }
         int end=i;
         String s = temp.substring(start,end+1);
         return s;
     }
     /**
     reverse temp from index i to index j
     @param temp to get a String
     @param i to get the start index
    @param j to get the end index
    @return to return the reversed String
     */
     public static String reverse(String temp,int i,int j)
     {
         char[] arr= temp.toCharArray();
         char mid;
         int m= j-1;                    
         for(int n=i;n<j;n++)
         {
             mid = arr[n-1];
             arr[n-1]=arr[m];
             arr[m--]=mid;
         }
         String s = String.valueOf(arr);
         return s;
     }
     /**
    return the times a str showed in another str
    @param t1 to get the longer String
    @param t2 to get the shorter String
    @return to return the times t2 shown in t1
     */
     public static int timesShow(String t1,String t2)
     {
         int num=-1;
         int index=0;
         while (index!=-1)
         {
             num++;
             if (num>=1) {
                 index=t1.indexOf(t2,t2.length()+index);
             }
             else
             {
                 index=t1.indexOf(t2,index);
             }
         } 
         return num;
     }
     /**
    find the greatest substring of str1 in str2 ever existed
    @param str1 to get the longer String
    @param str2 to get the shorter String
    @return to return the largest substring of str2 in str1
     */
     public static String minorStr(String str1,String str2)
     {    
         String temp;
         if(str1.contains(str2))
             return str2;
         int start=0;
         int end=str2.length()-1;
         int sublength=str2.length()-1;

         while (sublength!=0)
         {
             for(;end<=str2.length();start++,end++)
             {
                 char[] arr = str2.toCharArray();
                 if(str1.contains(str2.substring(start,end)))
                     return str2.substring(start,end);
             }
             sublength--;
             start=0;
             end=sublength;
             
         }
         return "";

     }
     /**
    System.out.println();
    @param temp to get the thing to be printed
     */
    public static void sop(Object temp)
    {
        System.out.println(temp);
    }
 }

     
/*
 class StrToolDemo
 {
     public static void main(String[] args) {
         String str1 = "lushan in the house do you know that";
         // String str2 = StrTool.myTrim(str1);
         // StrTool.sop(str2);
         // String str2 = StrTool.reverse(str1,2,4);
         //int num1 = StrTool.timesShow(str1,"lu");
         String str2 = "in the house";
         String str3 = StrTool.minorStr(str1,str2);
         StrTool.sop(str3);
     }
 }
 */

主要是自定义的字符串处理的class  自定义了本来就存在的trim函数 还有一些自定义的可以自己看说明书啊哈哈   不一一介绍了,最后main里面测试了一下,其实实现翻转的时候或许可以再封装一个函数

posted on 2016-04-07 15:07  fanfans1996  阅读(136)  评论(1编辑  收藏  举报