Scanner的概述与String类的构造和使用_DAY12

1:Scanner的概述(理解)

    1)Scanner是JDK5以后出现的方便我们从键盘接受数据的类。

    2)Scanner的构造格式:

       Scanner sc = new Scanner(System.in);

       System.in 是System类下面有一个静态的成员变量in。它的类型是InputStream。   

             代表的是标准键盘输入流。也就是键盘录入数据。

       Scanner是对其进行了封装,提供了各种转换功能。方便我们获取到想要的数据类型的数据。

    3)要掌握的两个功能:

       A:返回int类型

           public int nextInt()

       B:返回String类型

           a、 public String nextLine() //返回下一个完整标记前的内容,完整标记包含:回车

       例子:

      import java.util.Scanner;

           public class Demo1 {

              public static void main(String[] args) {

                  Scanner sc=new Scanner(System.in);

                  String s=sc.next();

                  System.out.println(s);  //输入:123 456   输出:123

                  System.out.println("----------------");

                  Scanner sc2=new Scanner(System.in); //输入:123 456   输出:123 456

                  String s2=sc2.nextLine();

                  System.out.println(s2);

              }

           }
View Code

            b、 public String next()  //返回下一个完整标记前的内容,完整标记包含:回车,空格

        例子:

  import java.util.Scanner;

        public class Demo1 {

           public static void main(String[] args) {

              Scanner sc=new Scanner(System.in);

              String s=sc.next();

              System.out.println(s);

           }

        }    

        

        输入:adfgrw131          输入:123 456 789   //有空格

        输出:adfgrw131          输出:123           //空格为完整的标记,注意输出的不同

 
View Code

       注意事项:

           先next,再nextLine会有问题。

           解决方案:

              重新建立Scanner对象。//一般不会这样做。因为消耗资源

              根据需求,选择合适的方法。

              统一一种方法。

2:String类的概述和使用(掌握)

     1)字符串数据类型。属于引用数据类型。

     2)String本质:值就是一个char[]

     3)String类的特殊性:

       字符串为常量,字符串值一旦初始化便不可以修改。

       常量池:在java用于保存在编译期已确定的,已编译的class文件中的一份数据。

       字符串特殊性内存解释。

         图String是常量

     4)String类的构造方法:

       A:String s = new String();

       B:String s = new String(byte[] bys);

       C:String s = new String(byte[] bys,int startIndex,int count);

       D:String s = new String(char[] chs);

       E:String s = new String(char[] chs,int startIndex,int count);

       F:String s = new String(String s2);

       G:String s = "hello";

       常使用:

           BCDEG

       例子:

package cn.itcast.myString;
/*
 * String本质:值就是一个char[]
 * String类型的构造方法:
 *         1:字面值
 *         2:String() 
 *         3:public String(byte[] bytes)  通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String
 *         4:public String(byte[] bytes,
              int offset,//offset:偏移量      距起始点差多少    即从哪开始
              int length)//length:长度        即取多少个元素               通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String
        5:public String(char[] value)分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。
        6:public String(char[] value,
              int offset,
              int count)                分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。
        7:public String(String original) 使用字符串创建字符串
 */
public class Demo3 {

    public static void main(String[] args) {
        
        //1:字面值
        String s = "abc";

        //2:String()  
        //没有值&null&有字符串对象但内容为空  比较  
        //String s2;  没有值
        String s3 = new String();
        s3 = null;   //有值,没有对象,值为null(空)
        String s4 = new String();  //有值,有对象,这个对象的值为空的。

        //3,4:通过字节数组创建字符串
        byte[] bArr = {97,98,99,-1,-2,-3};
        String s5 = new String(bArr);
        System.out.println(s5);  //abc??  ,java中2个字节确定一个char类型的字符,字节-1 ,-2对应一个字符?
        byte[] bArr2 = {97,98,99,100,101};
        String s6 = new String(bArr2,1,2);
        System.out.println(s6);  //bc
        s6 = new String(bArr2,1,bArr2.length-1);
        System.out.println(s6); //bcde
        
        //5,6:通过字符数组创建字符串
        char[] cArr = {'a','b','c','c'};
        String s7 = new String(cArr,2,2);
        System.out.println(s7); //cc
        
        char[] cArr2 = {97,98,99,100,101};
        String s8 = new String(cArr2,2,2);
        System.out.println(s8); //de
        
        //7:通过字符串创建字符串
        String s9 = new String("abc");
        System.out.println(s9);  //abc
    }

}
View Code

    5)String类的常用方法

    (1)、判断方法

       (1.1):public boolean equals(Object anObject)  类 Object 中的 equals,比较的为字符串的数值。

       (1.2):public boolean equalsIgnoreCase(String?anotherString)  Ignore:忽略  Case:大小写

       (1.3):public boolean isEmpty()  is:是     empty:空的

        例子 

package cn.itcast.myString;
/*
 * String的判断方法:
 *         1:public boolean equals(Object anObject)  类 Object 中的 equals,比较的为字符串的数值。
 *         2:public boolean equalsIgnoreCase(String anotherString)  Ignore:忽略  Case:大小写
 *         3:public boolean isEmpty()  is:是     empty:空的
 */
public class Demo4 {

    public static void main(String[] args) {

        //1:euqals方法被重写,比较值
        String s = "abc";
        String s2 = new String("abc");
        String s3 = "abc";
        
            System.out.println(s.equals(s2)); //比较内容
        
        System.out.println(s==s2); //flase  不同地址
        System.out.println(s==s3); //true  同一个地址
        
        //2:忽略大小写比较值
        System.out.println("=======================");
        String s4 = "Abc";
        String s5 = "abc";
        
        System.out.println(s4.equals(s5));
        System.out.println(s4.equalsIgnoreCase(s5));
        
        
        //3:判断字符串是否为空
        System.out.println("=======================");
        String s6 = new String();
        String s7 = null;  
        String s8 = "";
        
        System.out.println(s6.isEmpty());
        //System.out.println(s7.isEmpty());   空指针异常
        System.out.println(s8.isEmpty());
        System.out.println(s6==s8);
    }

}
View Code

       练习:  1:将一个int数组的值拼写成字符串形式。

                {1,2,3,4,5}

                [1,2,3,4,5]

package cn.itcast.myString;

/*
 * 1:将一个int数组的值拼写成字符串形式。
 * {1,2,3,4,5}
 * 
 * [1,2,3,4,5]
 */

public class Test {

    public static void main(String[] args) {
        
        int[] arr = {1,2,3,4,6};
        method(arr);
    }

    public static void method(int[] arr) {
        String s = "[";
        
        for (int i = 0; i < arr.length; i++) {
            /*
            int element = arr[i];
            
            if(i==arr.length-1) {
                s += element;
            }else {
                s += element+",";
//                s = s + element + ",";
            }
            */
            
            s += arr[i];
            if(i!=arr.length-1);
                s += ",";
        }
        
        s += "]";
        System.out.println(s);
    }
    
    
}
View Code

       (1.4)public boolean contains(CharSequence s)  当且仅当此字符串包含指定的 char 值序列时,返回 true。即A串里是否有子串

       (1.5)public boolean startsWith(String prefix)  以某个字符串开头

       (1.6)public boolean startsWith(String prefix, int toffset) 指定开始判断的偏移,是否以某个字符串开头

       (1.7)public boolean endsWith(String suffix)  以某个字符串结束

         例子 

package cn.itcast.myString;
/*
 * String的判断方法2:
 *         1:public boolean contains(CharSequence s)  当且仅当此字符串包含指定的 char 值序列时,返回 true。即A串里是否有子串
 *         2:public boolean startsWith(String prefix)  以某个字符串开头
 *         3:public boolean startsWith(String prefix, int toffset) 指定开始判断的偏移,是否以某个字符串开头
 *         4:public boolean endsWith(String suffix)  以某个字符串结束
 */
public class Demo5 {

    public static void main(String[] args) {
        //1:注意多态
        String s = "我爱java";
        String s2 = "java";
        
        boolean contains = s.contains(s2);   //String类实现了CharSequence的接口
        System.out.println(contains);
        
        //2,3:从指定索引开始判断
        System.out.println("===================");
        String s3 = "i love java,more and more!~";
        String s4 = "more";
        String s5 = "i";
        String s6 = "i love";
        String s7 = "ilove";
        
        System.out.println(s3.startsWith(s4));  //false
        System.out.println(s3.startsWith(s5));  //ture
        System.out.println(s3.startsWith(s6));  //true
        System.out.println(s3.startsWith(s7));  //false
        System.out.println("===================");
        System.out.println(s3.startsWith(s4,12)); //true ,字符串‘i love java,’的长度为12
        System.out.println(s3.startsWith(s5,12)); //flase
        System.out.println(s3.startsWith(s6,12)); //false
        System.out.println(s3.startsWith(s7,12)); //flase
        
        //4:
        System.out.println("===================");
        String s8 = "i love java,more and more";
        String s9 = "more";
        String s10 = "i love";
        
        System.out.println(s8.endsWith(s9));   //true
        System.out.println(s8.endsWith(s10));  //false

    }
}
View Code

    (2)、字符串的获取功能

       (2.1):public int length()  获取字符串长度的 《方法()》!  数组是获取数组长度属性!

       (2.2):public char charAt(int index) 通过索引获取字符

       (2.3):public int indexOf(int ch)   根据字符获取索引

       (2.4):public int indexOf(int ch,int fromIndex)

       (2.5):public int indexOf(String str) 根据字符串获取索引

       (2.6):public int indexOf(String str, int fromIndex)

       (2.7):lastIndexOf对应indexOf

       (2.8):public String substring(int beginIndex)  返回一个新的字符串,它是此字符串的一个子字符串。

       (2.9):public String substring(int beginIndex, int endIndex) 返回一个新的字符串,它是此字符串的一个子字符串。注意:包含头,不包含尾!

          例子:

package cn.itcast.myString;
/*
 * 字符串的获取功能:
 *         1:public int length()  获取字符串长度的 《方法()》!  数组是获取数组长度属性!
 *         2:public char charAt(int index) 通过索引获取字符
 *         3:public int indexOf(int ch)   根据字符获取索引
 *         4:public int indexOf(int ch,int fromIndex)
 *         5:public int indexOf(String str) 根据字符串获取索引
 *         6:public int indexOf(String str, int fromIndex)
 *         7:lastIndexOf对应indexOf
 *         8:public String substring(int beginIndex)  返回一个新的字符串,它是此字符串的一个子字符串。
 *         9:public String substring(int beginIndex, int endIndex) 返回一个新的字符串,它是此字符串的一个子字符串。
 */
public class Demo6 {

    public static void main(String[] args) {
        //1:获取字符串与获取数组长度代码对比
        String s = "i love java";
        String[] sArr = {"i","love","java"};
        
        System.out.println(s.length());
        System.out.println(sArr.length);
        
        //2:
        String s2 = "i love java";
        int index = 1;
        System.out.println(s2.charAt(index));
        String s3 = "中国";
        System.out.println(s3.charAt(index));
        
        //3,4,5,6,7:indexOf(XXX)  根据字符(串)获取索引   返回第一次找到的索引  返回的永远是在整个字符串的索引,与从哪里开始检索无关
        System.out.println("============================");
        String s4 = "中国信息化建设程度赶超美国!";
        
        int indexOf = s4.indexOf('国');
        System.out.println(indexOf);
        
        int indexOf2 = s4.indexOf("国!");
        System.out.println(indexOf2);
        
        int indexOf3 = s4.indexOf('国',4);
        System.out.println(indexOf3);
        
        int indexOf4 = s4.indexOf("国!",4);
        System.out.println(indexOf4);
        System.out.println("============================");
        
        System.out.println(s4.lastIndexOf('国'));
        System.out.println(s4.lastIndexOf("国!"));
        System.out.println(s4.lastIndexOf('国',4));
        System.out.println(s4.lastIndexOf("国!",4));
        
        //8:public String substring(int beginIndex)  返回一个新的字符串,它是此字符串的一个子字符串。
        //9:public String substring(int beginIndex, int endIndex) 返回一个新的字符串,它是此字符串的一个子字符串。包含头不包含尾!
        
        String s5 = "abcde i love java!";
        
        String substring = s5.substring(5);
        System.out.println(substring);
        String substring2 = s5.substring(0);
        System.out.println(substring2);  //原来的字符串
        
        String substring3 = s5.substring(0, 7);
        System.out.println(substring3);
        String substring4 = s5.substring(0,s5.length());
        System.out.println(substring4);
        
        System.out.println(s5);
    }

}
View Code

    (3)、字符串的转换功能

        (3.1):重写了Object的toString方法,返回这个字符串本身的数值

       (3.2):public static String copyValueOf(char[] data, int offset, int count)   //返回指定数组中表示该字符序列的 String。                  

        (3.3):public static String valueOf(boolean b或其他基本类型)  基本类型直接转成引用类型

         (3.4):public static String valueOf(char[] data,int offset,int count)

        (3.5):public static String valueOf(Object obj) //注意:字符数组特殊性:打印时,结果不是地址,而是值

         例子:

package cn.itcast.myString;

import cn.itcast.Person;

/*
 * String转换:
 *         1:重写了Object的toString方法,返回这个字符串本身的数值
 *         2:public static String copyValueOf(char[] data,   返回指定数组中表示该字符序列的 String。
                                 int offset,  //从哪开始
                                 int count)   //取多少个
                                 
        3:public static String valueOf(boolean b或其他基本类型)  基本类型直接转成引用类型
        4:public static String valueOf(char[] data,int offset,int count) 
        5:public static String valueOf(Object obj) 
        
        注意:
           字符数组特殊性:打印时,结果不是地址,而是值
 */
public class Demo7 {

    public static void main(String[] args) {

        //2:
        char[] cArr = {97,98,99};
        
        String copyValueOf = String.copyValueOf(cArr,1,2);
        System.out.println(copyValueOf);//bc
        
        //3:public static String valueOf(boolean b或其他基本类型)
        String valueOf = String.valueOf(true);  
        System.out.println(valueOf); //true
        
        //4:public static String valueOf(char[] data,int offset,int count)  
        char[] cArr2 = {'a','b','c'};
        String valueOf2 = String.valueOf(cArr2, 0, cArr2.length);
        System.out.println(valueOf2); //abc
        
        //5:public static String valueOf(Object obj) 
        Person p = new Person();
        String valueOf3 = String.valueOf(p);
        System.out.println(valueOf3); //调用p对象的toString方法
        
        int[] iArr = {1,2,3,4};
        char[] cArr3 = {'a','b','g','s'};
        
        String valueOf4 = String.valueOf(iArr);
        String valueOf5 = String.valueOf(cArr3); 
        
        System.out.println(valueOf4);  //打印出来地址
        System.out.println(valueOf5);//abgs
    }

}
View Code 

      (3.6):public String toLowerCase()  大变小

        (3.7):public String toUpperCase()   小变大

        (3.8):public String concat(String str)  将指定字符串连接到此字符串的结尾,可以直接用+代替

        例子:

package cn.itcast.myString;
/*
 *  String转换2:
 *      1:public String toLowerCase()  大变小
 *      2:public String toUpperCase()  小变大
 *      3:public String concat(String str)  将指定字符串连接到此字符串的结尾,可以直接用+代替
 */
public class Demo8 {

    public static void main(String[] args) {
        //1,2:
        String s = "I love java";
        
        String lowerCase = s.toLowerCase();
        System.out.println(lowerCase);
        
        String upperCase = s.toUpperCase();
        System.out.println(upperCase);
        
        System.out.println(s);
        
        //3:
        String s2 = ", very much!";
        System.out.println(s.concat(s2));
        System.out.println(s+s2);  //+代替concat()
        
    }

}
View Code

       (3.9)public char[] toCharArray()  转成字符数组

       (3.10)public byte[] getBytes()  转换成字节数组

        例子:

package cn.itcast.myString;
/*
 *  String的转换方法3:
 *      1:public char[] toCharArray()  转成字符数组
 *      2:public byte[] getBytes()  转换成字节数组
 */
public class Demo11 {

    public static void main(String[] args) {

        //1:
        String s = "i love java";
        char[] charArray = s.toCharArray();
        //{'i',' ','l','o','v','e'....}
        System.out.println(charArray);

        //2:
        String s2 = "ab";
        byte[] bytes = s2.getBytes();

        for (int i = 0; i < bytes.length; i++) {
            System.out.print(bytes[i] + " ");  //输出对应的ASCII码  97 98
        }
        System.out.println();
        String s3 = "我爱a";
        byte[] bytes2 = s3.getBytes();
        for (int i = 0; i < bytes2.length; i++) {
            System.out.print(bytes2[i] + " ");  //-50 -46 -80 -82 97  注意:一个中文字符对应两个字节 
        }
    }

}
View Code

    (4)、其他功能

          (4.1):public String replace(char oldChar, char newChar)  用新字符替换旧字符

         (4.2):public String[] split(String regex)   用指定规则分隔字符串

         例子:

package cn.itcast.myString;
/*
 * String的其他方法:
 *     1:public String replace(char oldChar, char newChar)  用新字符替换旧字符
 *     2:public String[] split(String regex)   用指定规则分隔字符串
 */
public class Demo9 {

    public static void main(String[] args) {

        //1:
        String s = "i love java?";
        String replace = s.replace('?', '!');
        String replace2 = s.replace('a', 'b');
        System.out.println(replace);//    i love java!
        System.out.println(replace2);//  i love jbvb?        

        //2:使用给定的regex规则,将调用split方法的对象分隔成字符串数组
        String s2 = "i wanna u";
        String[] split = s2.split(" ");  //用空格分割
        //{"i","wanna","u"}
        
        for (int i = 0; i < split.length; i++) {
            System.out.print(split[i]+"----");
        }
        
    }

}
View Code

          (4.3):public String trim() 返回字符串的副本,忽略前导空白和尾部空白。

 *        (4.4):public int compareTo(String anotherString) 按字典顺序比较两个字符串。先比较字母,如果一个字符串是另一个字符串的子字符串,

                                                            则比较长度

          例子:

package cn.itcast.myString;
/*
 * String的其他方法:
 *         1:public String trim() 返回字符串的副本,忽略前导空白和尾部空白。 
 *         2:public int compareTo(String anotherString) 按字典顺序比较两个字符串。先比较字母,如果一个字符串是另一个字符串的子字符串,则比较长度
 */
public class Demo10 {

    public static void main(String[] args) {

        //1:
        String s = "i love java";
        String s2 = "       i love java";
        String s3 = "i love java ";
        String s4 = "     i love java     ";
        
        System.out.println(s.trim());
        System.out.println(s2.trim());
        System.out.println(s3.trim());
        System.out.println(s4.trim());
        
        //2按字典顺序比较,public int compareTo(String anotherString) 按字典顺序比较两个字符串。
        String s5 = "abc";
        String s6 = "bbc";
        String s7 = "cbc";
        
        System.out.println(s5.compareTo(s5));
        System.out.println(s5.compareTo(s6));
        System.out.println(s6.compareTo(s5));
        
        System.out.println(s5.compareTo(s7));
        System.out.println(s7.compareTo(s5));
        
        String s8 = "abc";
        String s9 = "abcd";
        String s10 = "abcdefg";
        
        System.out.println(s8.compareTo(s8)); //0
        System.out.println(s8.compareTo(s9)); //-1
        System.out.println(s9.compareTo(s8)); //1
        
        System.out.println(s8.compareTo(s10));//-4
        System.out.println(s10.compareTo(s8));//4
        
        String s11 = "abcc";
        String s12 = "zbc";
        
        System.out.println(s11.compareTo(s12));  //-25
        System.out.println(s12.compareTo(s11));  //25
        
        String s13 = "aac";
        String s14 = "abc";
        
        System.out.println(s13.compareTo(s14)); //-1
    }

}
View Code

3、面试题:

       A:字符串一旦被赋值就不能被改动。

           注意:这里的改动指的是字符串的内容,而不是字符串对象的引用。

       B:String s = new String("hello");和String s = "hello";有区别吗?是什么呢?

           有。

           前者创建了两个对象。

           后者创建了一个对象。

       C:看程序,写结果

           String s1 = new String("hello");

           String s2 = new String("hello");

           System.out.println(s1==s2);  //比较地址,false

           System.out.println(s1.equals(s2)); // 比较内容,true

           String s3 = new String("hello");

           String s4 = "hello";

           System.out.println(s3==s4);   // 比较地址,false

           System.out.println(s3.equals(s4));  // 比较内容,true

           String s5 = "hello";

           String s6 = "hello";

           System.out.println(s5==s6);  //常量池里面,比较地址 true

           System.out.println(s5.equals(s6));  //常量池里面,比较内容 true

       D:看程序,写结果

           String s7 = "hello";

           String s8 = "world";

           String s9 = "helloworld";

           System.out.println(s9==s7+s8);   //false

           System.out.println(s9=="hello"+"world");  //true

           变量就直接造,常量先找,如果有就使用,否则就造。

    (4)字符串的常见功能:(补齐中文)

       A:判断功能

           boolean equals(Object obj)

           boolean equalsIgnoreCase(String str)

           boolean contains(String str)

           boolean startsWith(String str)

           boolean endsWith(String str)

           boolean isEmpty()

       B:获取功能

           int length()

           char charAt(int index)

           int indexOf(int ch)

           int indexOf(String str);

           int indexOf(int ch,int fromIndex)

           int indexOf(String str,int fromIndex)

           String substring(int start)

           String substring(int start,int end)

       C:转换功能

           byte[] getBytes()  //获取字符串的字节数组

           char[] toCharArray()  //获取字符串的字符数组

           static String copyValueOf(char[] chs)

           static String valueOf(char[] chs)

           static String valueOf(int i)

           String toLowerCase()

           String toUpperCase()

           String concat(String str)

       D:其他功能

           String replace(char old,char new)

           String replace(String old,String new)

           String[] split(String regex)

           String trim()

           int compareTo(String str)

           int compareToIgnoreCase(String str)

    (5)案例:(理解)

       A:遍历字符串

package cn.itcast;
/*
 * 遍历字符串中每一个字符
 */
public class Test {

    public static void main(String[] args) {
            
        String s = "i love java";
        
        for (int i = 0; i < s.length(); i++) { //遍历字符串
            char charAt = s.charAt(i);  //通过索引获取到每一个字符
            System.out.print(charAt+" ");
        }
        
        System.out.println();
        
        char[] charArray = s.toCharArray();//将字符串转成字符数组
        for (int i = 0; i < charArray.length; i++) {  //遍历数组
            char c = charArray[i];
            System.out.print(c);
        }
    }

}
View Code

       B:统计字符串中大写字母,小写字母以及数字字符出现的次数

package cn.itcast;
public class Main {
    public static void main(String[] args) {
        String s = "hello123ABC";
        //统计字符串中大写字母,小写字母以及数字字符出现的次数
        char[] c=  s.toCharArray();
        int uCount=0;
        int lCount=0;
        int numCount=0;

        for(int i=0;i<c.length;i++){
            if(c[i]>='A'&& c[i]<='Z'){
                uCount++;
            }else if (c[i]>='a'&& c[i]<='z'){
                lCount++;
            }else if(c[i]>='0'&& c[i]<='9'){
                numCount++;
            }
        }
        System.out.println(uCount);
        System.out.println(lCount);
        System.out.println(numCount);
    }
}
View Code

       C:把一个字符串的首字母变成大写,其他的全部小写

package cn.itcast;
public class Main {
    public static void main(String[] args) {
        String s = "I lovE Java";
        //截取头
        tset(s);
    }

    private static void tset(String s) {
        String headString=s.substring(0,1);

        //头变大写
        String uHeadString= headString.toUpperCase();

        //截取身体
        String bodyString= s.substring(1);
        //身体大写
        String uBodyString=bodyString.toLowerCase();
        //输出
        System.out.println(uHeadString+uBodyString);
    }
}
View Code

        D:统计大串中小串出现的次数

package cn.it;
/*
 * 给定一个字符串找到子串在字符串中出现的次数。String s = “abcitcastabcxxxabc”中的“abc”
 */
public class Test4 {

    public static void main(String[] args) {
        
        //定义变量,记录大字符串
        String s = "abcitcastabcxxxabc";
//                      "itcastabcxxxabc"
        //定义变量,记录小字符串
        String subS = "abc";
        
        method(s, subS);
    }
    public static int method(String s, String subS) {
        //定义变量,为初始字符串保持副本
        String copyS = s;
        //定义变量,记录出现次数
        int count = 0;
        
        //定义变量,记录小字符串在大字符串中出现的索引
        int index = s.indexOf(subS);
        
        //
        while (index!=-1) {
            count++;//找到一次小字符串就将个数+1
//            index+subS.length()  这是去掉了一次小字符串后的剩余字符串开始索引
//            截取剩余字符串
            s = s.substring(index+subS.length());
//            使用剩余字符串继续判断是否有"abc"字符串
            index = s.indexOf(subS);
        }
        
        System.out.println(subS+"在"+copyS+"中出现的次数为:"+count);

    }

}
View Code
原文地址:https://www.cnblogs.com/hezhiyao/p/7532455.html