12.常见对象(Scanner(用于接收键盘录入数据),String(字符串))

1.Scanner的概述和构造方法原理

1.Scanner的概述:    JDK5以后用于获取用户的键盘输入
2.Scanner的构造方法原理
    Scanner(InputStream source)
    System类下有一个静态的字段:
        public static final InputStream in; 标准的输入流,对应着键盘录入。

public class ScannerDemo {
    public static void main(String[] args) {
      /*  Scanner(InputStream source)
        构造一个新的 Scanner,它生成的值是从指定的输入流扫描的。*/
        InputStream in = System.in;
      /*

       System 类
       静态的属性  in
        public static final InputStream in“标准”输入流。此流已打开并准备提供输入数据。通常,此流对应于键盘输入*/

        Scanner scanner = new Scanner(in);

                //hasXXX()
               // nextXXXX()
        //录入数字的
        int i = scanner.nextInt();
        long l = scanner.nextLong();
        byte b = scanner.nextByte();
        boolean b1 = scanner.nextBoolean();
        double v = scanner.nextDouble();

        //录入字符串的
        String s = scanner.nextLine();

    }
}

2.Scanner类的hasNextXxx()和nextXxx()方法的讲解

1.基本格式
    hasNextXxx()  判断下一个是否是某种类型的元素,其中Xxx可以是Int,Double等。
                  如果需要判断是否包含下一个字符串,则可以省略Xxx
    nextXxx()  获取下一个输入项。Xxx的含义和上个方法中的Xxx相同

判断输入的数据是不是该类型(只判断一次):

public class MyTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个整数");
        // sc.hasNextInt() 判断录入的数据,是不是该类型
        if (sc.hasNextInt()) {
            int i = sc.nextInt();
            System.out.println(i);
        }else{
            System.out.println("输入类型的错误,我们需要录入一个整数,请重新录入。");
        }
    }
}

判断输入的数据是不是该类型(知道判断正确结束):

public class MyTest2 {
    public static void main(String[] args) {
        while (true){
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入一个整数");
            if (sc.hasNextInt()) {
                int i = sc.nextInt();
                System.out.println(i);
                break;
            } else {
                System.out.println("输入类型的错误,我们需要录入一个整数,请重新录入。");
            }
        }
    }
}

写成工具类时:

工具类:

public class ScannerUtils {
    private ScannerUtils() {}
    public static int getNum(){
        int num=0;
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入一个整数");
            if (sc.hasNextInt()) {
                 num = sc.nextInt();
                break;
            } else {
                System.out.println("输入类型的错误,我们需要录入一个整数,请重新录入。");
            }
        }
        return num;
    }
}

测试类:

public class MyTest3 {
    public static void main(String[] args) {
        int num = ScannerUtils.getNum();

        System.out.println(num);
    }
}

3.Scanner获取数据出现的小问题及解决方案

1.两个常用的方法: ​
public int nextInt():获取一个int类型的值 ​
public String nextLine():获取一个String类型的值 ​
public String next():获取一个String类型的值

2.案例演示
    a:先演示获取多个int值,多个String值的情况
    b:再演示先获取int值,然后获取String值出现问题
    c:问题解决方案
        第一种:先获取一个数值后,在创建一个新的键盘录入对象获取字符串。
        第二种:把所有的数据都先按照字符串获取,然后要什么,你就对应的转换为什么。

先录入整数,再录入字符串,字符串录不上的解决办法:

public class ScannerDemo2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个整数");
        int i = sc.nextInt();
        System.out.println(i);
        sc=new Scanner(System.in);
        System.out.println("录入一个字符串");
        String s = sc.nextLine();
        //String s = sc.next();  //asdfasdfas asfasdfasdf  asdfasdf
        System.out.println(s);
        //当我们先录入整数,再录入字符串,发现字符串没有录入上。
        //原因:当我们录入了整数后,敲了一下回车换行,那么这个回车换行,被当做字符串已经录入了。
        /*怎么解决
        *  1.再录入字符串时,重新创建一个新的Scanner对象
        * 2.换个录入字符串的方法 next();
        *
        * */



    }
}

nextLine()方法和next()方法的区别:

public class ScannerDemo3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请录入字符串");
        //String s = sc.nextLine();
        //sc.nextLine();  //会录入,回车换行,空格字符
       String s = sc.next(); //不录入 回车换行,空格字符
        System.out.println(s);


    }
}

4.String类的概述

1.什么是字符串
    字符串是由一个或多个字符组成的一串数据(字符序列)
    字符串可以看成是字符数组
2.String类的概述    
    通过JDK提供的API,查看String类的说明
    
    可以看到这样的两句话。
    a:字符串字面值"abc"也可以看成是一个字符串对象。
    b:字符串是常量,一旦被创建,就不能被改变。值不能被改变,你能改变的是引用或者说指向。

public class MyTest {
    public static void main(String[] args) {
        /*

        A:什么是字符串
	        字符串是由一个或多个字符组成的一串数据(字符序列)
	        字符串可以看成是字符数组  char[] s=new char[]{'a','b','c'}
        * String 类代表字符串。
        * 1.Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。
        * 2.字符串是常量;它们的值在创建之后不能更改。
        * */
        //字符串的构方法
       /* String()
        初始化一个新创建的 String 对象,使其表示一个空字符序列。
        */

        String s = new String(); //""

        System.out.println(s.toString()); //String类重写了toString()方法,会输出字符串的内容
        // "" 空的字符串
        System.out.println("");

     /*   String(String original)
        初始化一个新创建的 String 对象,使其表示一个与参数相同的字符序列;换句话说,新创建的字符串是该参数字符串的副本。*/

        String s1 = new String("abc");
        System.out.println(s1);

    }
}


字符串字面值"abc"也可以看成是一个字符串对象:

public class MyTest {
    public static void main(String[] args) {
         /*
        * String 类代表字符串。
        * 1.Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。
        * 2.字符串是常量;它们的值在创建之后不能更改。
        * */

        int length1 = "abc".length();
        System.out.println(length1);

        String s2 = new String("abc");
        int length = s2.length();
        System.out.println(length);


        String s3="abc";
        int length2 = s3.length();


    }
}

5.String类的构造方法

1.常见构造方法
    public String():空构造
    public String(String original):把字符串常量值转成字符串
    public String(byte[] bytes):把字节数组转成字符串    
    public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串(index:表示的是从第几个索引开始, length表示的是长度)
    public String(char[] value):把字符数组转成字符串
    public String(char[] value,int index,int count):把字符数组的一部分转成字符串
    
2.案例演示    
    演示String类的常见构造方法
    
    演示前先说一个字符串的方法:
        public int length():返回此字符串的长度。

不同的构造方法:

public class MyTest2 {
    public static void main(String[] args) {
        //public String( byte[] bytes):把字节数组转成字符串
        byte[] bytes = {97, 98, 99, 100};
        String s = new String(bytes);
        //abcd
        System.out.println(s);
        //从0索引处转换字节数组中4个字节
        String s2= new String(bytes,0,4);
        System.out.println(s2);

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

        //把字符数组转换成字符串。
        char[] chars = {'你', '好', '呀','好','好','学','习'}; //"你好呀"
         //System.out.println(""+'你');
       // String s3=""+chars[0]+chars[1]+chars[2];
       // System.out.println(s3);

        String s4 = new String();
        for (int i = 0; i < chars.length; i++) {
            s4+=chars[i];
        }
        System.out.println(s4);

        System.out.println("==========================================");
        //把字符数组转换成字符串。
        String s5 = new String(chars);
        System.out.println(s5);
        //转换字符数组的一部分,从3索引开始转换4个字符
        String s6 = new String(chars, 3, 4);
        System.out.println(s6);

        //length() 方法,获取字符串的长度
        int length = s6.length();
        System.out.println(length);

    }
}

 

6.String的特点一旦被创建就不能改变

1.String的特点:    一旦被创建就不能改变 因为字符串的值是在堆内存的常量池中划分空间 分配地址值的
2.案例演示    
    a:如何理解这句话
        String s = "hello" ; 
        s =  "world" + "java"; 问s的结果是多少?
    b: 内容不能变,引用可以变

public class MyTest2 {
    public static void main(String[] args) {
        /*
         * String 类代表字符串。
         * 1.Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。
         * 2.字符串是常量;它们的值在创建之后不能更改。
         * */
        String s = "hello";

        s = "world" + "java";

        System.out.println(s);//wordjava
    }

 

7.String类的常见面试题

A:面试题1
    String s = new String(“hello”)和String s = “hello”;的区别
    并画内存图解释。
B:面试题2
    ==和equals()的区别?   
C:面试题3
    看程序写结果
    String s1 = new String("hello");
    String s2 = new String("hello");
    System.out.println(s1 == s2);
    System.out.println(s1.equals(s2));

    String s3 = new String("hello");
    String s4 = "hello";
    System.out.println(s3 == s4);
    System.out.println(s3.equals(s4));

    String s5 = "hello";
    String s6 = "hello";
    System.out.println(s5 == s6);
    System.out.println(s5.equals(s6));

A:

public class MyTest3 {
    public static void main(String[] args) {
      //  String s = new String("hello") 和String s = "hello";的区别



        String s = "hello"; //构建一个Hello对象
        //构建两个对象
        String s1 = new String("abc");
    }
}

 


B:

public class MyTest {
    public static void main(String[] args) {
      /*  B:
        面试题2
                == 和equals() 的区别 ?*/

      /*
      *  ==是一个比较运算符,可以比较基本数据类型和引用数据类型。
      *  == 比较基本数据类型,比较的是两个值是否相等 int a=20;  int b=10;  a==b
      *  == 比较的是引用类型,比较的是两个对象的地址值是否相同。
      *
      *  equals() 是Object 类中的一个方法,默认是比较两个对象的地址值是否相同。
      *  有很多类会重写 equals()方法,会按照自己的比较方式去比较。
      * 比如我们自定义的类,重写  equals()方法 会比较两个对象的成员变量的值是否相同
      * String类 重写  equals()方法比较的是两个字符串,字面上的内容是否相同。
      *
      * */
    }
}

C: 

public class MyTest6 {
    public static void main(String[] args) {
        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
        //字符串类,重写了父类的equals()方法,比较的是,字符串字面上的内容是否相同。
        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
    }
}

public class MyTest5 {
    public static void main(String[] args) {
        String s="abc";
        String s2="abc";
        String s3 = new String("abc");
        System.out.println(s==s2); //true
        System.out.println(s==s3); //false
    }
}


练习题: 

public class MyTest2 {
    public static void main(String[] args) {
        String s1 = "Hello";
        String s2 = "Hello";
        String s3 = "Hel" + "lo"; // 编译器会优化,编译期可以确定
        String s4 = "Hel" + new String("lo");
        String s5 = new String("Hello");
        String s6 = s5.intern();
        String s7 = "H";
        String s8 = "ello";
        String s9 = s7 + s8;
        System.out.println(s1 == s2);  //true
        System.out.println(s1 == s3);  //true
        System.out.println(s1 == s4);  //false
        System.out.println(s1 == s9);  //false
        System.out.println(s4 == s5);  //false
        System.out.println(s1 == s6);  //true
    }
}

8.String类的判断功能

String类的判断功能
    public boolean equals(Object obj):                比较字符串的内容是否相同,区分大小写
    public boolean equalsIgnoreCase(String str):        比较字符串的内容是否相同,忽略大小写
    public boolean contains(String str):                判断字符串中是否包含传递进来的字符串
    public boolean startsWith(String str):                判断字符串是否以传递进来的字符串开头
    public boolean endsWith(String str):                判断字符串是否以传递进来的字符串结尾
    public boolean isEmpty():                        判断字符串的内容是否为空串""。

public class MyTest2 {
    public static void main(String[] args) {
        // A:
        // String类的判断功能
        // public boolean equals (Object obj):比较字符串的内容是否相同, 区分大小写
        // public boolean equalsIgnoreCase (String str):比较字符串的内容是否相同, 忽略大小写
        // public boolean contains (String str):判断字符串中是否包含传递进来的字符串
        // public boolean startsWith (String str):判断字符串是否以传递进来的字符串开头
        // public boolean endsWith (String str):判断字符串是否以传递进来的字符串结尾
        // public boolean isEmpty ():判断字符串的内容是否为空串 ""。
        // B:
        // 案例演示:
        // 案例演示String类的判断功能;

        System.out.println("abc".equals("Abc"));
        System.out.println("abc".equalsIgnoreCase("Abc"));

        System.out.println("我爱你们".contains("你们5"));


        boolean b = "王五".startsWith("王");
        System.out.println(b);

        boolean b1 = "王老虎".endsWith("虎");

        System.out.println(b1);

        boolean empty = " ".isEmpty();
        System.out.println(empty);

        boolean b2 = "adfasdf".length() == 0 ? true : false;

    }
}

9.模拟用户登录

需求:模拟登录,给三次机会,并提示还有几次。

public class MyTest3 {
    public static void main(String[] args) {
        // A:
        // 案例演示:
        // 需求:模拟登录, 给三次机会, 并提示还有几次。
        //数据库查出的
        String username="zhangsan";
        String password="123456";
        //使用用户输入的和数据库查出来的进行比对
        for (int i = 1; i <= 3; i++) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入用户名");
            String uname = sc.nextLine();
            System.out.println("请输入你的密码");
            String pwd = sc.nextLine();
            if (uname.equals(username) && pwd.equals(password)) {
                System.out.println("登录成功");
                break;
            } else {
                if((3-i)==0){
                    System.out.println("3次机会用完,银行卡被回收");
                }else{
                    System.out.println("用户名和密码错误,请重新输入你还剩" + (3 - i) + "次机会");
                }
            }
        }
    }
}

10.String类的获取功能

1.String类的获取功能
    public int length():                获取字符串的长度。
    public char charAt(int index):        获取指定索引位置的字符
    public int indexOf(int ch):            返回指定字符在此字符串中第一次出现处的索引。
    public int indexOf(String str):        返回指定字符串在此字符串中第一次出现处的索引。
    public int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
    public int indexOf(String str,int fromIndex): 返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
        可以顺带提一下lastIndexOf系列
    public String substring(int start):        从指定位置开始截取字符串,默认到末尾。
    public String substring(int start,int end):    从指定位置开始到指定位置结束截取字符串。
2.案例演示
    案例演示String类的获取功能

public class MyTest {
    public MyTest() {
    }

    public static void main(String[] args) {
        //关于 获取的方法
        String str="abc";
        //获取字符串的长度
        int len = str.length();
        System.out.println(len);
        //跟据索引获取某个字符
        char c = str.charAt(str.length() - 1);
        System.out.println(c);


        String s1="西部开源教育科技有限公司";
        //根据起始索引和终止索引,从原字符串中,截取一部分字符串,返回。 含头不含尾
        String str2 = s1.substring(0, 4);
        System.out.println(str2);

        //从5索引处开始截取到末尾
        String substring = s1.substring(5);
        System.out.println(substring);

    }
}


public class MyTest2 {
    public static void main(String[] args) {
        //获取某个字符在原字符中第一次出现的索引位置,如果没检索到,返回 -1 你可以用 -1做判断。
        String s = "开西部开源教育科技开有限公司西部开源";
        int i = s.indexOf('我');
        System.out.println(i);

        int index = s.indexOf("西部开源");

        System.out.println(index);

        //从指定索引处开始查找
        int index2 = s.indexOf("西部开源",4);
        int index3 = s.indexOf("西部开源", s.indexOf("西部开源")+1);
        System.out.println(index2);
        System.out.println(index3);


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


        String s2 = "开西部开源教育科技开有限公司西部开源教育";
        int index4 = s2.lastIndexOf("教育");
        System.out.println(index4);
    }
}

 

11.字符串的遍历

public class MyTest {
    public static void main(String[] args) {
        //字符串的遍历
        //字符串也是编有索引的,从0开始
        //"abcdefafdsfasdfadsf"
        //字符串底层,用字符数组来存储的。
        String str="abcefefgenh";
        //根据索引获取单个字符
        char c = str.charAt(0);
        System.out.println(c);
        System.out.println("==================================================");
        for (int i = 0; i < str.length(); i++) {
            char c1 = str.charAt(i);
            System.out.println(c1);
        }

        System.out.println("===============================");
        for (int j = str.length()-1; j >= 0; j--) {
            char c1 = str.charAt(j);
            System.out.println(c1);
        }
    }
}

 

12.统计不同类型字符个数

 需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)

public class MyTest3 {
    public static void main(String[] args) {
       /* A:
        案例演示:
        需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)*/

        String str = "aaaAAA888";
        int da = 0;
        int xiao = 0;
        int num = 0;
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (ch >= 'A' && ch <= 'Z') {
                da++;
            } else if (ch >= 'a' && ch <= 'z') {
                xiao++;
            } else if (ch >= '0' && ch <= '9') {
                num++;
            }
        }
        System.out.println("大写字母有"+da+"个");
        System.out.println("小写字母有" + xiao + "个");
        System.out.println("数字字符有" + num + "个");
    }
}

13.String类的转换功能

1.String的转换功能:
    public byte[] getBytes():                        把字符串转换为字节数组。
    public char[] toCharArray():                    把字符串转换为字符数组。
    public static String valueOf(char[] chs):            把字符数组转成字符串。
    public static String valueOf(int i):                把int类型的数据转成字符串。
        注意:String类的valueOf方法可以把任意类型的数据转成字符串。
    public String toLowerCase():                    把字符串转成小写。
    public String toUpperCase():                    把字符串转成大写。
    public String concat(String str):                    把字符串拼接。
2.案例演示
    案例演示String类的转换功能

public class MyTest {
    public static void main(String[] args) {
        //把字符串转换成字节数组
        String str = "abcdefg";
        byte[] bytes = str.getBytes();
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(bytes[i]);
        }

        String s = new String(bytes);
        System.out.println(s);


        System.out.println("=========================");
        String s1 = "我"; //UTF-8 一般汉字会占 3个字节  GBK 一个汉字占两个字节
        byte[] bytes1 = s1.getBytes();
        for (int i = 0; i < bytes1.length; i++) {
            System.out.println(bytes1[i]);
        }

        byte[] newByte = {-26, -120, -111};

        String s2 = new String(newByte);
        System.out.println(s2);

    }
}

public class MyTest2 {
    public static void main(String[] args) {
        //把字符串转换成字符数组
        String str="西部开源教育科技有限公司"; //char[] chars={'西','部'}

        /*
        char[] chars = new char[str.length()];

        for (int i = 0; i < str.length(); i++) {
          chars[i]= str.charAt(i);
        }

        */

        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            System.out.println(chars[i]);
        }

        //把字符数组转换成字符串

        String s = new String(chars);
        System.out.println(s);

    }
}

public class MyTest3 {
    public static void main(String[] args) {
        int num=100; //"100"
        double num2=3.14;//"3.14"
        boolean f=false; //"false"

        String s=num+"";
        String s2=num2+"";
        String s3=f+"";

        String s1 = String.valueOf(num);
        System.out.println(s1);

        String s4 = String.valueOf(true); //"true"

        String s5 = String.valueOf(3.5);  //"3.5"

        String s6 = String.valueOf(new char[]{'a', 'b', 'c'});
        String s7 = new String(new char[]{'a', 'b', 'c'});


        String s9="abc";
        String s8 = s9.toUpperCase();
        System.out.println(s8);
        String s10="EFG";
        String s11 = s10.toLowerCase();
        System.out.println(s11);

        //拼接字符串

        String str2="aaa"+"bbb"+"ccc";

        String sss = "AAA".concat("BBB");


        String concat = "AAA".concat("BBB").concat("CCC").concat(str2);
        System.out.println(concat);

    }
}

14.按要求转换字符

需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)

public class MyTest {
    public static void main(String[] args) {
        // A:
        // 案例演示:
        // 需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)
        String str = "abcASFEadsfasdfasdfEFDFEF";
       /*
        String substring = str.substring(0, 1);
        String s = substring.toUpperCase();
        String substring1 = str.substring(1);
        String s1 = substring1.toLowerCase();

        String concat = s.concat(s1);
        System.out.println(concat);
*/

        //链式编程
        String s = str.substring(0, 1).toUpperCase().concat(str.substring(1).toLowerCase());
        System.out.println(s);


    }
}

15.String类的其他功能

1.String的替换功能及案例演示
    public String replace(char old,char new)            将指定字符进行互换
    public String replace(String old,String new)        将指定字符串进行互换
2.String的去除字符串两空格及案例演示
    public String trim()                            去除两端空格
3.String的按字典顺序比较两个字符串及案例演示
    public int compareTo(String str)    会对照ASCII 码表 从第一个字母进行减法运算 返回的就是这个减法的结果
                        如果前面几个字母一样会根据两个字符串的长度进行减法运算返回的就是这个减法的结果
                        如果连个字符串一摸一样 返回的就是0
    public int compareToIgnoreCase(String str) 跟上面一样 只是忽略大小写的比较 

public class MyTest2 {
    public static void main(String[] args) {
        String str="奥巴马和特朗普是美国总统";

      /*  String s= str.replace("奥巴马", "********");
        System.out.println(s);
        String replace = s.replace("特朗普", "xxxx");
        System.out.println(replace);*/

        String s = str.replace("奥巴马", "********").replace("特朗普", "xxxx");
        System.out.println(s);


        String str2="    a  b  c     ";
        //去除字符串左右两端的空格
        String trim = str2.trim();
        System.out.println(trim);
    }
}


public class MyTest3 {
    public static void main(String[] args) {
       /* public int compareTo (String str)会对照ASCII 码表 从第一个字母进行减法运算 返回的就是这个减法的结果
        如果前面几个字母一样会根据两个字符串的长度进行减法运算返回的就是这个减法的结果
        如果连个字符串一摸一样 返回的就是0*/

        int i = "abc".compareTo("Abcdef");
        System.out.println(i);

        //不区分大小写
        i = "abc".compareToIgnoreCase("Abc");

        System.out.println(i);

    }
}

16.去除字符串左端空格

public class MyTest {
    public static void main(String[] args) {
        String str="       asdfasdfsa  ddfdf  fasdfasd        ";
        int index=0;
        String substring=null;
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if(ch==' '){
                index++;
            }else{
                System.out.println(i);
                 substring = str.substring(index);
                break;
            }
        }
        //System.out.println(index);

        System.out.println(substring);
    }
}

17.去除中间空格

public class MyTest {
    public static void main(String[] args) {
        String str="asdf   asdf as           fas             fas        d         as     df   ass  df      asdf      as  d   fas    df     asdfasd";
        System.out.println(str.replace(" ", ""));
    }
}

18.判断一个字符串,在原串中只出现过一次

public class MyTest {
    public static void main(String[] args) {
        String str2 = "西部开源教育科技有限公司西部开源科技教育有限公司西部开源科技有限公司";

        if (str2.indexOf("教育") == str2.lastIndexOf("教育")) {
            System.out.println("出现过一次");
        } else {
            System.out.println("不止一下");
        }
    }
}

19.字符串反转并断点查看

需求:把字符串反转
        举例:键盘录入"abc"        
        反转结果:"cba"

public class MyTest4 {
    public static void main(String[] args) {
       /* A:
        案例演示
        需求:把字符串反转
        举例:键盘录入 "abc"
        反转结果:"cba"*/

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一段字符串");
        String s = scanner.nextLine();
        String s1="";
        for (int i = s.length()-1; i >= 0; i--) {
           s1+=s.charAt(i);
        }
        System.out.println(s1);
    }
}

20.把数组转成字符串

需求:把数组中的数据按照指定个格式拼接成一个字符串
        举例:
            int[] arr = {1,2,3};    
        拼接结果:
            "[1, 2, 3]"

public class MyTest3 {
    public static void main(String[] args) {
        /*
        A:
        案例演示
        需求:把数组中的数据按照指定个格式拼接成一个字符串
        举例:
        int[] arr = {1, 2, 3};
        拼接结果:
        "[1, 2, 3]"
         */

        int[] arr = {1, 2, 3,4,5,6,7};
        String str="[";
        for (int i = 0; i < arr.length; i++) {
            if(i==arr.length-1){
                str+=arr[i]+"]";
            }else{
                str+=arr[i]+",";
            }
        }
        System.out.println(str);
    }
}

21.统计大串中小串出现的次数

public class MyTest5 {
    public static void main(String[] args) {
      /*  A:
        画图演示
        需求:统计大串中小串出现的次数
        举例:
        "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun" 中java出现了5次*/


        String maxStr = "woaijavawozhenaijavawozhendeaijavawozhjavaendehenaijavaxinbuxinwoaijavagun";
        String minStr = "java";

        int index = maxStr.indexOf(minStr);
        int count=0;
        while (index!=-1){
            count++;
            maxStr=maxStr.substring(index+4);
            index = maxStr.indexOf(minStr);
        }
        System.out.println(count);
    }
}

public class MyTest6 {
    public static void main(String[] args) {
        String maxStr = "woaijavawozhenaijavawozhendeaijavawozhjavaendehenaijavaxinbuxinwoaijavagun";
        String minStr = "java";

        int count=0;
        if (!maxStr.contains("#")) {
            String str = maxStr.replace("java", "#");
            for (int i = 0; i < str.length(); i++) {
                char ch = str.charAt(i);
                if (ch == '#') {
                    count++;
                }
            }

            System.out.println(count);
        }
    }
}

public class MyTest7 {
    public static void main(String[] args) {
        String maxStr = "woaijavawozhenaijavawozhendeaijavawozhjavaendehenaijavaxinbuxinwoaijavagun";
        int length = maxStr.length();
        String java = maxStr.replace("java", "");
        int length1 = java.length();
        int num= (length-length1)/4;
        System.out.println(num);
    }
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值