[JAVA Learning String] Notes

Some basic knowledge of strings.
Insert image description here
Learned API
Scanner
Random

String overview

Insert image description here
Insert image description here
Demo

        //1.使用直接赋值的方式获取一个字符串对象(使用最多)
        String s1 = "abc";
        System.out.println(s1);//abc


        //2.使用new的方式来获取一个字符串对象
        //空参构造:可以获取一个空白的字符串对象
        String s2 = new String();
        System.out.println("@" + s2 + "!");//""

        //传递一个字符串,根据传递的字符串内容再创建一个新的字符串对象
        String s3 = new String("abc");
        System.out.println(s3);

        //传递一个字符数组,根据字符数组的内容再创建一个新的字符串对象
        //需求:我要修改字符串的内容。  abc  Qbc
        //abc -->  {'a','b','c'}  -->  {'Q','b','c'} --> "Qbc"
        char[] chs = {
    
    'a', 'b', 'c', 'd'};
        String s4 = new String(chs);
        System.out.println(s4);//abcd

        //传递一个字节数组,根据字节数组的内容再创建一个新的字符串对象
        //应用场景:以后在网络当中传输的数据其实都是字节信息
        //我们一般要把字节信息进行转换,转成字符串,此时就要用到这个构造了。
        byte[] bytes = {
    
    97, 98, 99, 100};
        String s5 = new String(bytes);
        System.out.println(s5);//abcd

Insert image description here
Insert image description here

string comparison

Insert image description here
Insert image description here
Insert image description here
code

        //1.创建两个字符串对象
        String s1 = new String("abc");
        String s2 = "Abc";

        //2.==号比较
        //基本数据类型:比的是数据值
        //引用数据类型:比的是地址值
        System.out.println(s1 == s2);//false


        //3.比较字符串对象中的内容是否相等
        boolean result1 = s1.equals(s2);
        System.out.println(result1);

        //4.比较字符串对象中的内容是否相等,忽略大小写
        //1 一 壹 这不行
        //忽略大小写只能是英文状态下的a A
        boolean result2 = s1.equalsIgnoreCase(s2);
        System.out.println(result2);//true

The typed string is different from the string defined in the code

public static void main(String[] args) {
    
    
        //1.假设我现在键盘录入一个abc
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串");
        String str1 = sc.next();//abc  是new出来的。
        //2.代码中再定义一个字符串abc
        String str2 = "abc";
        //3.用==比较,这两者能一样吗?
        System.out.println(str1 == str2);//false

        //结论:
        //以后只要想比较字符串的内容,就必须要用String里面的方法

Screenshot software called Snipaste

Practice user login

Use one account to log in, you cannot log in more than 3 times

//
        String rightUsername = "zhangsan";
        String rightPassword = "123456";

        Scanner sc = new Scanner(System.in);


        for (int i = 0; i < 3; i++) {
    
    
            System.out.println("请输入用户名");
            String username = sc.next();
            System.out.println("请输入密码");
            String password = sc.next();

            if (username.equals(rightUsername) && password.equals(rightPassword)){
    
    
                System.out.println("成功登录");
                break;

            }else {
    
    
                if (i == 2){
    
    
                    System.out.println("账号"+username+"已被锁定");
                }else {
    
    
                    System.out.println("用户登录失败,用户名或密码有误,您还剩" + (2-i) + "次机会");
                }

            }

        }

StringBuilder

StringBuilder can be regarded as a container. After creation, the content inside is variable.
Function: Improve the efficiency of string operations.
Scenarios for using StringBuilder :
//1. String concatenation
//2. String reversal.
Insert image description here
Insert image description here
Top three method demo

        //1.创建对象
        StringBuilder sb = new StringBuilder();

        //2.添加元素
        sb.append(1);
        sb.append(3.4);
        sb.append(true);

        //反转
        sb.reverse();

        //获取长度
        int len = sb.length();
        System.out.println(len);

        //打印
        //普及:
        //因为StringBuilder是Java已经写好的类
        //java在底层对他做了一些特殊处理。
        //打印对象不是地址值而是属性值。
        System.out.println(sb);

Running results
Insert image description here
tostring() demonstration

        //1.创建对象
        StringBuilder sb = new StringBuilder();

        //2.添加字符串
        sb.append("aaa");
        sb.append("zzz");
        sb.append("fff");
        sb.append("hhh");

        System.out.println(sb);//只是容器内的排列顺序,不是字符串

        //3.再把StringBuilder变回字符串
        String str = sb.toString();
        System.out.println(str);

operation result
Insert image description here

chain programming

        //1.创建对象
        StringBuilder sb = new StringBuilder();

        /*//2.添加字符串
        sb.append("aaa");
        sb.append("zzz");
        sb.append("fff");
        sb.append("hhh");*/

        //链式编程
        sb.append("aaa").append("zzz").append("fff").append("hhh");

        System.out.println(sb);//只是容器内的排列顺序,不是字符串

        //3.再把StringBuilder变回字符串
        String str = sb.toString();
        System.out.println(str);

The running result is the same as last time

Exercise:
Determine whether the input string is symmetrical

        //1.键入字符串
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串");
        String str = sc.next();
        //2.反转字符串
        //借助StringBuilder中的reverse进行反转
        StringBuilder sb = new StringBuilder();//=右边为创建对象,赋值给sb
        sb.append(str);
        sb.reverse();
        String res = sb.toString();

        System.out.println(sb);


        //也可写做
        //String res = new StringBuilder().append(str).reverse().toString();

        //3.比较
        if (str.equals(res)){
    
    
            System.out.println("该字符串是对称字符串");
        }else {
    
    
            System.out.println("该字符串不是对称字符串");
        }

Running results
Asymmetric
Insert image description here
Symmetric
Insert image description here

StringJoiner

Insert image description here
Insert image description here
Note here that StringJoiner does not construct
Insert image description here
an instance

        //1.创建对象
        StringJoiner sj = new StringJoiner("~~~","{","}");

        //2.添加元素
        sj.add("zzz").add("fff").add("hhh");

        int len = sj.length();
        System.out.println(len);

        //3.打印
        System.out.println(sj);

        String str = sj.toString();
        System.out.println(str);

result
Insert image description here
Insert image description here

TIPS

ctrl+alt+T: You can choose a function to wrap the selected code
ctrl+shift+/: Block comment
ctrl+/: Line comment
Insert image description here

Guess you like

Origin blog.csdn.net/Luohuasheng_/article/details/131697521