String String class common algorithm problem

1. A string is inverted. The specified portion of the string is inverted.

 1 public class StringDemo {
 2     //方式一:转换为char[]
 3     public String reverse(String str,int startIndex,int endIndex){
 4 
 5         if(str != null){
 6             char[] arr = str.toCharArray();
 7             for(int x = startIndex,y = endIndex;x < y;x++,y--){
 8                 char temp = arr[x];
 9                 arr[x] = arr[y];
10                 arr[y] = temp;
11             }
 12 is  
13 is              return  new new String (ARR);
 14          }
 15          return  null ;
 16      }
 . 17  
18 is      // Second way: String using splicing 
. 19      public String reverse1 (STR String, int startIndex, int endIndex) {
 20 is          IF (STR =! null ) {
 21 is              // part. 1 
22 is              String reverseStr str.substring = (0 , startIndex);
 23 is              // part 2 
24              for ( intendIndex = I; I> = startIndex; i-- ) {
 25                  reverseStr + = str.charAt (I);
 26 is              }
 27              // Part 3 
28              reverseStr str.substring + = (+ endIndex. 1 );
 29  
30              return reverseStr ;
 31 is  
32          }
 33 is          return  null ;
 34 is      }
 35      // three ways: using StringBuffer / StringBuilder replacement String 
36      public String reverse2 (STR String, int startIndex, int endIndex) {
 37 [          IF (STR =!null){
38             StringBuilder builder = new StringBuilder(str.length());
39 
40             //第1部分
41             builder.append(str.substring(0,startIndex));
42             //第2部分
43             for(int i = endIndex;i >= startIndex;i--){
44 
45                 builder.append(str.charAt(i));
46             }
47             //第3部分
48             builder.append(str.substring(endIndex + 1));
49 
50             return builder.toString();
51         }
52         return null;
53 
54     }
55 
56     @Test
57     public void testReverse(){
58         String str = "abcdefg";
59         String reverse = reverse2(str, 2, 5);
60         System.out.println(reverse);
61     }
62 
63 }

2. Get the number of occurrence of a string in another string.

 1 public class StringDemo {
 2     public int getCount(String mainStr,String subStr){
 3         int mainLength = mainStr.length();
 4         int subLength = subStr.length();
 5         int count = 0;
 6         int index = 0;
 7         if(mainLength >= subLength){
 8             //方式一:
 9 //            while((index = mainStr.indexOf(subStr)) != -1){
10 //                count++;
11 //                = mainStr.substring mainStr (+ subStr.length index ());
 12 is  //             }
 13 is              // two ways: one way to improve on 
14              the while (! (index = mainStr.indexOf (substr, index)) = -1 ) {
 15                  COUNT ++ ;
 16                  index + = subLength;
 . 17              }
 18 is  
. 19              return COUNT;
 20 is          } the else {
 21 is              return 0 ;
 22 is          }
 23 is      }
 24  
25      @Test
 26 is      public void testGetCount(){
27         String mainStr = "abkkcadkabkebfkaabkskab";
28         String subStr = "ab";
29         int count = getCount(mainStr, subStr);
30         System.out.println(count);
31     }
32 }

3. Get two strings largest same substring.

. 1  public  class StringDemo2 {
 2      // premise: a maximum only two strings the same substring 
. 3      public String getMaxSameString (str1 String, String str2) {
 . 4          IF (! Str1 = null ! && str2 = null ) {
 . 5              String MAXSTR ? = (str1.length ()> = str2.length ()) str1: str2;
 . 6              String minStr = (str1.length () <str2.length ())? str1: str2;
 . 7              int length = minStr.length ( );
 . 8  
. 9              for ( int I = 0; I <length; I ++ ) {
 10                  for (int X = 0, Y = length - I; Y <= length; ++ X, Y ++ ) {
 . 11                      String = substr minStr.substring (X, Y);
 12 is                      IF (maxStr.contains (substr)) {
 13 is                          return substr;
 14                      }
 15  
16                  }
 . 17              }
 18 is  
. 19          }
 20 is          return  null ;
 21 is      }
 22 is  
23 is      // if the same plurality of the same maximum length sequence existence
 24      // At this time to return String [], may be replaced with the rear set of ArrayList, more convenient 
25      public String[] getMaxSameString1(String str1, String str2) {
26         if (str1 != null && str2 != null) {
27             StringBuffer sBuffer = new StringBuffer();
28             String maxString = (str1.length() > str2.length()) ? str1 : str2;
29             String minString = (str1.length() > str2.length()) ? str2 : str1;
30 
31             int len = minString.length();
32             for (int i = 0; i < len; i++) {
33                 for (int x = 0, y = len - i; y <= len; x++, y++) {
34                     String subString = minString.substring(x, y);
35                     if (maxString.contains(subString)) {
36                         sBuffer.append(subString + ",");
37                     }
38                 }
39 //                System.out.println(sBuffer);
40                 if (sBuffer.length() != 0) {
41                     break;
42                 }
43             }
44             String[] split = sBuffer.toString().replaceAll(",$", "").split("\\,");
45             return split;
46         }
47 
48         return null;
49     }
50 
51     @Test
52     public void testGetMaxSameString(){
53         String str1 = "abcwerthello1yuiodefabcdef";
54         String str2 = "cvhello1bnmabcdef";
55         String[] maxSameStrings = getMaxSameString1(str1, str2);
56         System.out.println(Arrays.toString(maxSameStrings));
57 
58     }
59 
60 }

Guess you like

Origin www.cnblogs.com/ZengBlogs/p/12176544.html