关于String的统计字符出现次数及字符串的反转

统计一个字符串中某个字符出现的次数

  • replace方法统计字符串中某个字符出现的次数
    • 利用replace替换空字符,并用原字符串长度减去替换后的新字符串的长度
  • split方法统计字符串中某个字符出现的次数
    • 利用split分割,并统计分割后的数组长度

replace方法统计字符串中某个字符出现的次数---代码如下: 

  /**
     * @Title:replaceCountTimes
     * @date:2020年5月5日 下午2:18:38  
     * @Description:TODO replace方法统计字符串中某个字符出现的次数
     * void
     */
    public static void replaceCountTimes () {
        // 原字符串
        String str = "ABC123ABC";
        // 某个字符
        String searchChar = "B";
        // 统计次数初始为0
        int count = 0;
        // 原字符串的长度
        int origialLength = str.length();
        // 把某个字符替换为"",生成新字符串
        str = str.replace(searchChar, "");
        // 出现次数 = 原字符串的长度-新字符串的长度
        count = origialLength - str.length();
        System.out.println("字符" + searchChar + "出现的次数为:" + count);
    }

split方法统计字符串中某个字符出现的次数---代码如下:

  /**
     * @Title:splitCountTimes
     * @date:2020年5月5日 下午2:27:47  
     * @Description:TODO  split方法统计字符串中某个字符出现的次数
     * void
     */
    public static void splitCountTimes () {
        // 原字符串
        String str = "ABC123ABC";
        /**
         * --1.split根据某个字符分割成数组
         * --2.出现次数 = 分割后的数组长度
         */
        int count = str.split("B").length;
        System.out.println("字符" + str + "出现的次数为:" + count);
    }

实现字符串的反转

  • StringBuilder方法反转字符串
    • 利用StringBuilder的reverse
  • Stack方法反转字符串
    • 利用栈的先进后

StringBuilder方法反转字符串---代码如下:

  /**
     * @Title:StringBuilderReverseStr
     * @date:2020年5月5日 下午2:40:46  
     * @Description:TODO StringBuilder方法反转字符串
     * void
     */
    public static void StringBuilderReverseStr () {
        // 原字符串
        String str = "ABC123ABC";
        System.out.println("反转前:" + str);
        // 把原字符串转为StringBuilder
        StringBuilder stringBuilder = new StringBuilder(str);
        // StringBuilder的字符串反转方法
        stringBuilder.reverse();
        // 反转后字符串
        str = stringBuilder.toString();
        System.out.println("反转后:" + str);
    }

Stack方法反转字符串---代码如下:

  /**
     * @Title:stackReverseStr
     * @date:2020年5月5日 下午2:41:47  
     * @Description:TODO Stack方法反转字符串
     * void
     */
    public static void stackReverseStr () {
        // 原字符串
        String str = "ABC123ABC";
        System.out.println("反转前:" + str);
        // 转为char数组
        char[] charArray = str.toCharArray();
        // new 出一个Stack
        Stack<Character> stack = new Stack<>();
        // 遍历char数组并顺序放入Stack中
        for (char item : charArray) {
            // Pushes an item onto the top of this stack
            stack.push(item);
        }
        // 初始原字符串为""
        str = "";
        /**
         * --1.遍历char数组
         * --2.利用Stack的后进先出
         * --3.即为反转后字符串
         */
        for (int i = 0; i < charArray.length; i++) {
            // Removes the object at the top of this stack and returns that object as the value of this function
            str+=stack.pop();
        }
        System.out.println("反转后:" + str);
    }
原文地址:https://www.cnblogs.com/mjtabu/p/12830845.html