黑马程序员JAVA基础String 类(下)

一:模拟一个trim 方法 , 去除字符串两端的空格

  1、判断字符串第一个位置是否是空格,如果是继续向下判断,知道不是空格为止。 结尾也是。

  2、当开始和结尾都判断到不是空格时,就是要获取的字符。

 1 public class TrimText {
 2     public static void main(String args[])
 3     {
 4         String str = "  There are no accidents.       " ; 
 5         System.out.println(MyTrim(str)) ;
 6     }  
 7     public static String MyTrim(String str)
 8     {
 9         int start = 0 , end = str.length() - 1  ; 
10         
11         while(start <= end && str.charAt(start) == ' ')
12                 start ++;
13         while(end >= start && str.charAt(end) == ' ')  
14                 end -- ;
15         return str.substring(start , end + 1) ; 
16     }
17 }

  在第11行和第13行判断字符串开始和结尾的非空格字符的角标。

二:将字符串进行反转和将字符串中指定部分进行反转。

  1、将字符串转换成字符数组。

  2、将字符数组反转。

  3、将反转的字符数组转换成字符串。

代码如下:

 1 public class Text2 {
 2     public static void main(String args[])
 3     {
 4         String str = "There are no accidents." ; 
 5         System.out.println(ReverseString(str));
 6         System.out.println(ReverseString(str,0,5));
 7     }
 8 //    将部分字符串反转
 9     public static String ReverseString(String str , int start , int end)
10     {
11 //        将字符串转换成字符数组:
12         char[] ch = str.toCharArray() ; 
13 //        将数组反转: 
14         Reverse(ch , start , end) ;
15         return String.copyValueOf(ch) ;
16     }
17 //    定义字符串反转函数:
18     public static String ReverseString(String str)
19     { 
20         return ReverseString(str,0,str.length()) ;
21     }
22 //    反转函数:
23     public static void Reverse(char[] ch , int start , int end)
24     {
25         for (int i = start , j = end - 1 ; i < j ; i++ , j--)
26         {
27             swap(ch , i , j) ;
28         }
29     }
30 //    换位置函数 ; 
31     public static void swap(char[] ch , int i ,int j)
32     {
33         char temp  = ch[i] ; 
34         ch[i] = ch[j]  ;
35         ch[j] = temp ; 
36     } 
37 }

  总结:在编程时将功能细化。编写特定功能的方法,方便检查和调用。如上面代码,将字符数组的反转功能、位置交换等功能编写特定的方法。

三:获取一个字符串str在另一个字符串中出现的次数。

  1、定义一个计数器。

  2、获取str在另一字符串中第一次出现的位置。

  3、从第一次出现的位置后剩余的字符串中继续获取str在另一字符串出现的位置。

  4、当获取不到时,计数完成。

代码如下:

 1 public class Text3 {
 2 
 3     public static void main(String args[])
 4     {
 5         String str = "ababababababaccccceeeassefefadsfa" ;
 6         String str1 = "ab" ;
 7         System.out.println(Counter_2(str,str1));
 8         System.out.println(Counter(str,str1));
 9     }
10 //    定义计数器:
11     public static int Counter(String str , String str1 )
12     { 
13         if (str.length() < str1.length())
14             return 0 ;
15         int start = str.indexOf(str1) ;
16         if ( start == -1 ) 
17             return 0 ;
18         else 
19         { 
20             return  1 + Counter(str.substring(start + str1.length()) ,str1) ;
21         } 
22     }
23 //    定义计数器2
24     public static int Counter_2(String str , String str1)
25     {
26         int count = 0 ;
27         int index = 0 ; 
28         while (str.indexOf(str1,index) != -1)
29         {
30             index = index + str1.length() ;
31             count ++ ; 
32         }
33         return count ;
34     }
35 }

     注意:不能用split 方法来处理该问题。如果 str = "kkdfwefadfkkfaewf" , str1 = "kk" 时,用splik 方法会将 str 字符串分割成三份。 

四:获取两个字符串中最大相同子串。

  1、将短的那个字符串长度递减的方式获取到。

  2、将每一份获取到的子串去长串中判断是否是子串。如果是,则返回。

代码如下:

 1 public class Text4 {
 2     
 3     public static String GetMaxSubString(String str , String str1)
 4     {
 5         String Max = "" , Min = "" ; 
 6 //        判断谁长谁短
 7         Max = (str.length() > str1.length()) ? str : str1 ;
 8         Min = ( Max == str) ? str1 : str ;
 9         
10 //        将短的那个字符串长度递减的方式获取到。
11 //        DecreaseLength是递减的长度大小。
12 //        从0到 Min.length() - 1 ;
13         for(int DecreaseLength = 0 ; DecreaseLength < Min.length() ; DecreaseLength ++)
14         {
15 //              循环  start至end 长度的的子串去判断长字符串中是否包含。 
16             for (int start = 0 , end = Min.length() - DecreaseLength ;
17                 end != Min.length() ; end ++ , start ++ )
18             {
19                 String temp = Min.substring(start,end ) ; 
20                 if (Max.contains(temp))
21                     return temp ;
22             }
23         }
24         return "" ; 
25     } 
26     public static void main(String args[])
27     {
28         String str = "adsfwefahellocvbve" ; 
29         String str1 = "hellofqfadfx" ;
30         System.out.println(GetMaxSubString(str,str1));
31     }
32 }
原文地址:https://www.cnblogs.com/jbelial/p/2976621.html