String的练习题

1、将一个字符串进行反转,将一个字符串中指定的部分进行反转,比如“abcdefg”反转为“abfedcg"

方式1:转为char[]

   public String reverse(String str, int startIndex, int endIndex) {

        if (str != null) {
            char[] arr = str.toCharArray();

            for (int x = startIndex, y = endIndex; x < y; x++, y--) {
                char temp = arr[x];
                arr[x] = arr[y];
                arr[y] = temp;
            }

            return new String(arr);

        }
        return null;
    }

方式二:使用string拼接

 public String reverse1(String str, int startIndex, int endIndex) {
        if (str != null) {
            //1
            String reverseStr = str.substring(0, startIndex);
            //2
            for (int i = endIndex; i < startIndex; i++) {
                reverseStr += str.charAt(i);
            }
            //3
            reverseStr += str.substring(endIndex - 1);
            return reverseStr;
        }
        return null;
    }

方式三:使用StringBuffer 和 StringBuilder替换string

public String reverse2(String str, int startIndex, int endIndex) {
        if (str != null){
            //1
            StringBuilder builder = new StringBuilder(str.length());
            builder.append(str.substring(0,startIndex));
            //2
            for (int i = endIndex ;i>=startIndex;i++){
                builder.append(str.charAt(i));
            }
            //3
            builder.append(str.substring(endIndex +1));
            return builder.toString();
        }
        return null;
    }

调用任意一种方法

  String str = "abcdefg";
        String reverse = reverse(str, 2, 5);
        System.out.println(reverse);

2、 获取一个字符串在另一个字符串中出现的次数 比如 :”ab“在"absfabasdgabdadgsabaweavabagsdb"出现的次数


    public int getCount(String mainStr, String subStr) {
        int mainLength = mainStr.length();
        int subLength = subStr.length();
        int count = 0;
        int index = 0;
        if (mainLength >= subLength) {
            //1
//            while ((index = mainStr.indexOf(subStr)) != -1) {
//                count++;
//                mainStr = mainStr.substring(index + subStr.length());
//            }
            while ((index = mainStr.indexOf(subStr,index)) != -1) {
                count++;
                index += subLength;
            }
            return count;

        } else {
            return 0;
        }
    }
    @Test//单元测试方法
    public void  testGetCount(){
        String mainStr = "absfabasdgabdadgsabaweavabagsdb";
        String subStr = "ab";
        int count =getCount(mainStr,subStr);
        System.out.println(count);
    }

3、 获取两个字符串的最大相同子串

  public String getMaxSameString(String str1, String str2) {
        if (str1 != null && str2 != null) {
            String maxStr = (str1.length() >= str2.length()) ? str1 : str2;
            String minStr = (str1.length() <= str2.length()) ? str1 : str2;
            int length = minStr.length();
            for (int i = 0; i < length; i++) {
                for (int x = 0, y = length - i; y <= length; x++, y++) {
                    String subStr = minStr.substring(x, y);
                    if (maxStr.contains(subStr)) {
                        return subStr;
                    }
                }
            }


        }
        return null;
    }
    @Test
    public void test(){
        String str1 = "badsvashellowanndsan";
        String str2 = "afwahelloanig ";
        String maxSameString = getMaxSameString(str1,str2);
        System.out.println(maxSameString);
    }

你可能感兴趣的:(String的练习题)