A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 果蝇 中级黑马   /  2015-9-8 22:28  /  274 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public class StringDemo {
        /*
         * 其他功能: 1. A:String 的替换功能 public String replace(char old,char new)将指定字符进行互换
         * public String replace(String old,String new) 将指定字符串进行互换
         *
         *
         * 2. B:String 的去除字符串两空格 public String trim() 去除两端空格
         *
         * 3. C:String 的按字典顺序比较两个字符串及案例演示 public int compareTo(String str) public
         * int compareToIgnoreCase(String str)
         *
         * 案例: 练习1:把字符串反转 举例:键盘录入"abc" 反转结果:"cba",倒着遍历 练习2:统计大串中小串出现的次数,统计大串中java的次数
         * 举例:
         * "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavaYeah"中java出现了5次
         * 提示:
         * 1.使用contains判断,大串中是否包含小串
         * 2.如果包含,就substring,注意角标
         * 3.统计切割次数.再将返回来的新的字符串,再判断,再切割. 4.直到最后没有小的字符串为止.
         *
         * 加油啊,晚上争取做出来!!! YOU ARE BEST!!!
         */

        public static void main(String[] args) {

                //求什么就定义什么
                int count = 0;
                String big = "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavaYeah";
                String temp = big;
                String small ="java";
                // 判断 big 是否包含 small
//                boolean contains = big.contains(small);
                while(temp.contains(small)){
                        //获取角标
                        int indexOf = temp.indexOf(small);
                        //System.out.println(indexOf);
                        //切割字符串 ,需要角标
                        temp= temp.substring(indexOf+small.length());
                        System.out.println(temp);
                        count++;
                        System.out.println("---------"+count+"-------------");
                }
                        System.out.println("次数为" + count);
                       
                       
                       

                        // fun1();

                        // fun2();
                        // fun3();
                       
        }

        private static void fun3() {
                String s = "abceF";
                String s2 = "abcef";
                int compareTo = s.compareTo(s2);
                System.out.println(compareTo);
                System.out.println(s.compareToIgnoreCase(s2));
                // System.out.println('F'-'c');
        }

        // public int compareTo(String anotherString) {
        // int len1 = value.length; //当前字符串长度 ""
        // int len2 = anotherString.value.length; //参数字符串的长度
        // int lim = Math.min(len1, len2); // 比较出来一个小的
        // char v1[] = value; //当前字符串的数组
        // char v2[] = anotherString.value; // 参数字符串的数组
        //
        // int k = 0;
        // while (k < lim) {
        // char c1 = v1[k]; // 当前字符串第一个字符
        // char c2 = v2[k]; // 参数第一个字符
        // if (c1 != c2) {
        // return c1 - c2;
        // }
        // k++;
        // }
        // return len1 - len2;
        // }

        private static void fun2() {
                // 去除两端空格
                String s = "    alksjdlkajf  asf   asfd   ";
                String trim = s.trim();
                System.out.println(trim);
        }

        private static void fun1() {
                // replace

                String s = "helloworld";
                String replace = s.replace("l", "k");
                System.out.println(replace);
                System.out.println(s);
        }

}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马