Java 常见对象 02

常见对象·String类

Scanner 的概述和方法介绍

* A:Scanner 的概述

* B:Scanner 的构造方法原理
  * Scanner(InputStream source)
  * System 类下有一个静态的字段:
    * public static final ImputStream in; 标准的输入流,对应着键盘录入。

* C:一般方法:
  * hasNextXxx()  判断是否还有下一个输入项,其中Xxx可以是Int,Double等。如果需要判断是否包含下一个字符串,则可以省略Xxx

  * nextXxx()   获取下一个输入项。Xxx 的含义和上一个方法中的Xxx相同,默认情况下,Scanner使用空格,回车等作为分隔符

package com.heima.scanner;

import java.util.Scanner;

public class Demo1_Scanner {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in); // 键盘录入
        // int i = sc.nextInt(); // 键盘录入整数,存储在 i中
        // System.out.println(i);
        if (sc.hasNextInt()) {
            int i = sc.nextInt();
            System.out.println(i);
        } else {
            System.out.println("输入的类型错误");
        }

    }
}
Scanner

 

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

* A:两个常用的方法:
  * public int nextInt() :获取一个int类型的值
  * public String nextLine() :获取一个String类型的值

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

package com.heima.scanner;

import java.util.Scanner;

public class Demo2_Scanner {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        /*System.out.println("请输入第一个整数:");
        int i = sc.nextInt();
        System.out.println("请输入第二个整数");
        int j = sc.nextInt();
        System.out.println("i = " + i + ", j = " + j);*/
        
        /*System.out.println("请输入第一个字符串");
        String line1 = sc.nextLine();
        System.out.println("请输入第二个字符串");
        String line2 = sc.nextLine();
        System.out.println("line1 = " + line1 + ", line2 = " + line2);*/
        
        /*
         * nextInt() 是键盘录入整数的方法,当我们录入10的时候
         * 其实在键盘上录入的是 10 和 \r\n
         * nextInt() 只获取10就结束了
         * nextLine() 是键盘录入字符串的方法,可以接受任意类型
         * 遇到\r\n就证明字符串录入结束,并且不存储\r\n
         */
        
        /*System.out.println("请输入第一个整数:");
        int i = sc.nextInt();
        System.out.println("请输入第二个字符串");
        String line2 = sc.nextLine();
        System.out.println("i = " + i + ", line2 = " + line2);*/
        
        /*
         * 解决方案
         * 1,创建两次对象,浪费空间
         * 2,键盘录入的都是字符串,都用nextLine方法,后面我们会学习将整数字符串转换成整数的方法
         */
        System.out.println("请输入第一个整数:");
        int i = sc.nextInt();
        Scanner sc2 = new Scanner(System.in);
        System.out.println("请输入第二个字符串");
        String line2 = sc2.nextLine();
        System.out.println("i = " + i + ", line2 = " + line2);
    }

}
Scanner

 

String类的概述

* A:String 类 的概述
  * 通过JDK提供的API,查看String类的说明
  * 可以看到这样两句话:
    * a:字符串字面值 "abc" 可以被看成一个字符串对象
    * b:字符串是常量,一旦被赋值,就不能被改变

package com.heima.string;

public class Demo1_String {

    public static void main(String[] args) {
        //Person p = new Person();
        String str = "abc";  //字符串可以看坐一个字符串对象
        System.out.println(str);//String类重写了 toString()方法,使其返回该对象本身
        str = "def";    // 原来str记录的字符串 "abc" 变成了垃圾
        System.out.println(str);
    }

}
String

 

String类的构造方法

* A:常见构造方法
  * public String()  :空参构造
  * public String(byte [ ] bytes) :把字节数组转化为字符串
  * public String(byte [ ] bytes, int index, int length) :把字节数组的一部分转化为字符串
  * public String(char [ ] value) :把字符数组转化为字符串
  * public String(char [ ] value, int index, int count) :把字符数组的一部分转化为字符串
  * public String(String original) :把字符串常量值转化为字符串

* B:案例演示
  * 演示String类 的常见构造方法

package com.heima.string;

public class Demo2_String {

    public static void main(String[] args) {
        String s1 = new String();// 空参构造
        System.out.println(s1);

        byte[] arr1 = { 97, 98, 99 };
        String s2 = new String(arr1);// 把字节数组转成字符串
        System.out.println(s2); // 解码,将计算机读得懂的转换成我们能读得懂的

        byte[] arr2 = { 97, 98, 99, 100, 101, 121 };
        String s3 = new String(arr2, 2, 3);// 指定起始位置和字符串长度
        System.out.println(s3);

        char[] arr3 = { 'a', 'b', 'c', 'd', 'e' };
        String s4 = new String(arr3); // 将字符数组转换成字符串
        System.out.println(s4);

        String s5 = new String(arr3, 1, 3);// 指定起始位置和字符串长度
        System.out.println(s5);

        String s6 = new String("heima");// 将字符串转化为字符串
        System.out.println(s6);
    }

}
String

 

String类的常见面试题

* 1、判断定义为 String 类型的s1 和 s2 是否相等

String s1 = "abc";
String s2 = "abc";

System.out.println(s1 == s1);  // true
System.out.println(s1.equals(s2));  // true

 常量池中没有这个字符串对象就创建一个,如果有,就直接用
 Java中重写了equals方法,使其比较的是值而不是地址值

 

* 2、下面这句话在内存中创建了几个对象?

String s1 = new String("abc");  // 2

 创建了两个对象,一个在常量池中,一个在堆中

 

* 3、判断定义为String类型的s1 和 s2 是否相等

String s1 = new String("abc");
String s2 = "abc";
System.out.println(s1 == s2);  // false
System.out.println(s1.equals(s2));  // true

 

* 4、判断定义为String类型的s1 和 s2 是否相等

String s1 = "a" + "b" + "c";
String s2 = "abc";
System.out.println(s1 == s2);  // true
System.out.println(s1.equals(s2));  // true

Java中有常量优化机制,编译时将  "a" + "b" + "c" 识别成了 "abc"

 

* 5、判断定义为String类型的 s1 和 s2 是否相等

String s1 = "ab";
String s2 = "abc";
String s3 = s1 + "c";
System.out.println(s3 == s2);  // false
System.out.println(s3.equals(s2));  // true

 

底层会先创建一个StringBuilder(或 StringBuffer),然后将字符串 及其他数据类型 append 到 StringBuilder 中,再变成String对象

 

String类的判断功能

* A:String类的判断功能
  * boolean equals(Object obj) :比较字符串的内容是否相同,区分大小写
  * boolean equalsIgnoreCase(String str) :比较字符串的内容是否相同,忽略大小写
  * boolean contains(String str) :判断大的字符串中是否包含小的字符串
  * boolean startsWith(String str) :判断字符串是否以某个特定的字符串开头
  * boolean endsWith(String str) :判断字符串是否以某个指定的字符串结尾
  * boolean isEmpty() :判断字符串是否为空

package com.heima.string;

public class Demo4_StringMethod {

    public static void main(String[] args) {
        // demo1();
        // demo2();
        // demo3();
        //demo4();
    }

    private static void demo4() {
        String s1 = "heima";
        String s2 = "";
        String s3 = null;
        
        System.out.println(s1.isEmpty());
        System.out.println(s2.isEmpty());
        //System.out.println(s3.isEmpty());    // java.lang.NullPointerException
        
        /*
         * 判断是否为空,空格不是空,并且字符串不能是null
         * ""和null的区别 : 
         *       ""是字符串常量,同时也是一个String类的对象 ,既然是对象,当然就可以调用String类中的方法
         *       null是空常量,不能调用任何方法,否则会出现空指针异常,null常量可以给任意的引用数据类型赋值
         */
    }

    private static void demo3() {
        String s1 = "我爱你haha";
        System.out.println(s1.startsWith("我爱你"));// 判断是否为指定字符串开头
        System.out.println(s1.endsWith("e"));// 判断是否为指定字符串结尾
    }

    private static void demo2() {
        String s1 = "我爱你,haha";
        String s2 = "我爱";
        String s3 = "hehe";

        System.out.println(s1.contains(s2));// 判断字符串内是否包含小字符串
        System.out.println(s1.contains(s3));
    }

    private static void demo1() {
        String s1 = "heima";
        String s2 = "heima";
        String s3 = "HeiMa";

        System.out.println(s1.equals(s2)); // true
        System.out.println(s2.equals(s3)); // false 区分大小写
        System.out.println("---------------");
        System.out.println(s1.equalsIgnoreCase(s2)); 
        System.out.println(s1.equalsIgnoreCase(s3)); // 忽略大小写
    }

}
String

 

模拟用户登录

* A:案例演示
  * 需求:模拟登录,给三次机会,并提示还有几次
  * 用户名和密码都是 admin

package com.heima.test;

import java.util.Scanner;

public class Test1 {
    /*
     * 分析:
     * 1、模拟登录,需要键盘输入
     * 2、给三次机会,需要循环,for
     * 3、提示有几次机会,需要判断,if
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);    // 创建键盘录入对象
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入用户名:");
            String userName = sc.nextLine();    // 将键盘录入的用户名存储再userName中
            System.out.println("请输入密码:");
            String passwd = sc.nextLine();    // 将键盘录入的密码存储再passwd中
            // 如果是字符串常量和字符串变量比较,通常都是字符串常量调用方法,防止空指针异常
            if ("admin".equals(userName) && "admin".equals(passwd)) { 
                System.out.println("欢迎" + userName + "登录");
                break;
            } else {
                System.out.println("用户名 或者 密码 错误");
                if (i == 2) {
                    System.out.println("您的尝试次数已满,请您明天再来");
                } else {
                    System.out.println("输入错误,您还剩下" + (2 - i) + "次机会");
                }

            }
        }
    }

}
Test1

 

String类的获取功能

* A:String类的获取功能
  * int length() :获取字符串到长度
  * char charAt(int index) :获取指定索引位置的字符
  * int indexOf(int ch) :返回指定字符在此字符串中第一次出现处的索引
  * int indexOf(String str) :返回指定字符串在此字符串中的第一次出现处的索引
  * int indexOf(int ch, int fromIndex) :返回指定字符串在此字符串中从指定位置后第一次出现处的索引
  * lastIndexOf :从后向前找
  * String substring(int start) :从指定位置开始截取字符串,默认到末尾,会将截取后的字符串返回,原字符串实际不改变
  * String substring(int start, int end) :从指定位置开始到指定位置结束截取字符串

package com.heima.string;

public class Demo5_StringMethod {

    public static void main(String[] args) {
        // demo1();
        // demo2();
        // demo3();
        // demo4();        
    }

    private static void demo4() {
        String s1 = "heimawudi";
        String s2 = s1.substring(5);// 从指定位置开始,默认到最后,包含指定的那个索引值
        System.out.println(s2);
        String s3 = s1.substring(0, 5);// 包含头,不包含尾;左闭右开
        System.out.println(s3);    // substring会产生一个新的字符串,需要将新的字符产接收
    }

    private static void demo3() {
        String s1 = "woaiheima";
        int i = s1.indexOf('a', 3);// 从第三个开始向后找,包括第三个
        System.out.println(i);

        int d = s1.lastIndexOf('i');// 从后向前找,第一次出现的字符,但是索引值不变,找不到返回-1
        System.out.println(d);

        int e = s1.lastIndexOf('a', 7);// 从指定位置开始,由后向前寻找
        System.out.println(e);
    }

    private static void demo2() { // 根据字符找索引
        String s1 = "heima";
        int index1 = s1.indexOf(97);
        int index2 = s1.indexOf('a');// 参数接收int类型的,传入char类型的能自动提升
        System.out.println(index1);// 如果不存在,返回-1;如果存在,返回该字符第一次出现的位置
        System.out.println(index2);
        int index3 = s1.indexOf("ma");// 找字符串,如果匹配返回子串中第一个字符的位置,如果不匹配,返回-1
        System.out.println(index3);
    }

    private static void demo1() { // 根据索引找字符
        // int[] arr = { 11, 22, 33 };
        // System.out.println(arr.length); // 数组中的length是属性,没有()
        String s1 = "heima";
        System.out.println(s1.length()); // String中的length是方法,有()
        String s2 = "你要减肥,造吗?";
        System.out.println(s2.length());

        char c1 = s2.charAt(5);// 根据索引获取相应位置的字符
        System.out.println(c1);//
        // char c2 = s2.charAt(10); // java.lang.StringIndexOutOfBoundsException
        // System.out.println(c2); // 注意下标越界
    }

}
String

 

字符串的遍历

* A:案例演示
  * 需求:遍历字符串

Test2
package com.heima.test;

public class Test2 {
    public static void main(String[] args) {
        String s = "heima";
        for (int i = 0; i < s.length(); i++) { // 通过for循环获取到字符串中每个字符的索引
            // char c = s.charAt(i);
            // System.out.println(c);
            System.out.println(s.charAt(i)); // 通过索引获取每个字符
        }
    }
}

 

统计不同类型的字符个数

* A:案例演示
  * 需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数,其他字符出现的次数
  * 例:ABCDEafcd123456!@#$%^

package com.heima.test;

public class Test3 {
    /*
     * 分析:字符串都是由字符组成的,而字符的值都是有范围的,通过范围来判断是否包含该字符 如果包含,就让计数器变量自增
     */
    public static void main(String[] args) {
        String s = "ABCDEabcd123456!@#$%^";
        int big = 0;
        int small = 0;
        int num = 0;
        int other = 0;
        // 1、获取每一个字符,通过for循环
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i); // 通过索引获取每一个字符
            // 2、判断字符是否在某个特定范围内
            if (c >= 'A' && c <= 'Z') {
                big++; // 如果满足是大写字母的条件,就让其对应的变量自增
            } else if (c >= 'a' && c <= 'z') {
                small++;
            } else if (c >= '0' && c <= '9') { // 注意范围是0字符到9字符,而不是数字0到9
                num++;
            } else {
                other++;
            }
        }
        // 3、打印每一个计数器的结果
        System.out.println(s);
        System.out.println("大写字母有 " + big + "个");
        System.out.println("小写字母有 " + small + "个");
        System.out.println("数字有 " + num + "个");
        System.out.println("其他字符有 " + other + "个");
    }

}
Test3

 

String类的转换功能

* A:String的转换功能:

  * byte[ ] getBytes() :把字符串转换为字节数组
  * char[ ] toCharArray() :把字符串转换为字符数组
  * static String valueOf(char[ ] chs) :把字符数组转换成字符串
  * static String valueOf(int i) :把int类型的数据转换成字符串
    注意:String类的valueOf() 方法可以把任意类型的数据转换成字符串

  * String toLowerCase() :把字符串转成小写
  * String toUpperCase() :把字符串转成大写
  * String concat(String str) :把字符串拼接

package com.heima.string;

import com.heima.bean.Person;

public class Demo6_StringMethod {

    public static void main(String[] args) {
        // demo1();
        // demo2();
        // demo3();
        demo4();

    }

    private static void demo4() {
        String s1 = "heiMa";
        String s2 = "chengxvYUAN";
        System.out.println(s1.toLowerCase());// 全转小写
        System.out.println(s2.toUpperCase());// 全转大写
        System.out.println(s1 + s2);    // 用加号拼接字符串更强大,可以将字符串与任意类型相加
        System.out.println(s1.toLowerCase().concat(s2.toUpperCase()));// concat方法调用的和传入的都必须是字符串
    }

    private static void demo3() {
        char[] arr = { 'a', 'b', 'c' };
        String s = String.valueOf(arr);// 底层是由String类的构造方法完成的
        System.out.println(s);

        String s2 = String.valueOf(100);// 将数字转换为字符串
        System.out.println(s2 + 100); // 字符串相加,不是数字相加

        Person person = new Person("张三", 23);
        String s3 = String.valueOf(person);// 调用的是对象的toString方法
        System.out.println(s3);
        System.out.println(person);
    }

    private static void demo2() {
        String s = "heima";
        char[] arr = s.toCharArray(); // 将字符串转换为字符数组
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + "    ");
        }
    }

    private static void demo1() {
        String s1 = "abc";
        byte[] arr = s1.getBytes(); // 通过ASCII码表,将字符串转换成字节数组
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + "    ");
        }

        System.out.println();
        /*
         * 通过 gbk码表 或者 utf-8 码表 将字符串转换成字节数组 编码:把我们看得懂的转换为计算机看得懂的 gbk码表中,一个中文代表两个字节
         * gbk码表特点,中文的第一个字节肯定是负数,第二个字节有可能是正的
         */
        String s2 = "你好你好";
        byte[] arr2 = s2.getBytes();
        for (int i = 0; i < arr2.length; i++) {
            System.out.print(arr2[i] + "    ");
        }
        System.out.println();
    }

}
String

 

按要求转换字符 (链式编程思想)

* A:案例演示:
  * 需求:把一个字符串的首字母转换成大写,其余为小写(只考虑英文大小写字母字符)

package com.heima.test;

public class Test4 {
    /*
     * 链式编程:
     * 只要保证每次调用完方法返回的是对象,就可以继续调用
     */
    public static void main(String[] args) {
        String s = "woaiHEImaNiAIMA";
        String s2 = s.substring(0,1).toUpperCase().concat(s.substring(1).toLowerCase());
        System.out.println(s2);
    }
}
Test4

 

把数组转换成字符串

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

package com.heima.test;

public class Test5 {
    /*
     * 分析:
     * 1、需要定义一个字符串 "["
     * 2、遍历数组获取每一个元素
     * 3、用字符串与数组中的元素进行拼接
     */
    public static void main(String[] args) {
        int[] arr = { 1, 2, 3 };
        String s = "[";    // 定义一个字符串用来与数组中的元素拼接

        for (int i = 0; i < arr.length; i++) {// {1, 2, 3}
            if (i == arr.length - 1) {
                s = s + arr[i] + "]";    // [1, 2, 3]
            } else {
                s = s + arr[i] + ", ";    // [1, 2, 
            }
        }
        System.out.println(s);
    }
}
Test5

 

String类的其他功能

* A:替换功能
  * String replace(char old, char new)
  * String reolace(String old, String new)

* B:去除字符串两边空格
  * String trim()

* C:按字典顺序比较两个字符串
  * int compareTo(String str) (讲集合TreeSet时细讲)
  * int compareToIgnoreCase(String str)

package com.heima.string;

public class Demo7_StringMethod {

    public static void main(String[] args) {
        // demo1();
        // demo2();
        demo3();
    }

    private static void demo3() {
        String s1 = "abc";
        String s2 = "bcd";

        int num = s1.compareTo(s2); // 按照unicode码表,从左开始逐个比较
        System.out.println(num);

        String s3 = "黑";
        String s4 = "黑马";
        System.out.println(s3.compareTo(s4));

        String s5 = "heima";
        String s6 = "HEIMA";
        System.out.println(s5.compareToIgnoreCase(s6)); // 不区分大小写
    }

    private static void demo2() {
        String s = "   hei   ma   ";
        System.out.println(s.trim());// 除去两端的空格
        System.out.println(s);
    }

    private static void demo1() {
        String s = "heimaheima";
        String s2 = s.replace('a', 'o'); // 字符替换,如果不存在指定字符,就保留原字符
        System.out.println(s2);
        String s3 = s.replace("ei", "ao"); // 字符串替换
        System.out.println(s3);
    }
}
String

 

字符串反转

* A:案例演示:
  * 需求:把字符串反转
    * 举例:键盘录入"abc"
    * 输出结果:"cba"

package com.heima.test;

import java.util.Scanner;


public class Test6 {
    /*
     * 分析:
     * 1、通过键盘录入获取字符串Scanner
     * 2、将字符串转换成字符数组
     * 3、倒着遍历字符数组,并再次拼接成字符串
     * 4、打印
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);    // 创建键盘录入对象
        System.out.println("请输入一个字符串");
        String str = sc.nextLine();    // 将键盘录入的字符串存储在line中
        String s = "";

        char[] arr = str.toCharArray();    // 将字符串转换为字符数组
        for (int i = arr.length - 1; i >= 0; i--) {
            s += arr[i];    // 拼接成字符串,实际上,每次都在生成新的字符串,产生大量垃圾
        }
        System.out.println(s);
        
    }
    
}
Test6

 

在大串中查找小串出现的次数

* A:画图演示
  * 需求:统计大串中小串出现的次数
  * 这里的大串和小串可以自己给出

* B:案例演示
  * 统计大串中小串出现的次数

package com.heima.test;

public class Test7 {

    public static void main(String[] args) {
        String max = "woaiheima,heimabaima,zhaodaogongzuojiushihaoma"; // 定义大串
        String min = "heima"; // 定义小串
        int index = 0; // 定义索引
        int count = 0; // 定义计数器变量
        while ((index = max.indexOf(min)) != -1) { // 定义循环,判断小串是否在大串中出现
            count++;
            max = max.substring(index + min.length());
        }
        System.out.println(count);
    }

}
Test7

 

posted @ 2020-03-31 13:05  小么VinVin  阅读(177)  评论(0编辑  收藏  举报