Java 字符串

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import java.util.Scanner;

public class Test3 {
    public static void main(String[] args) {
        // 1、字符串的拼接
//        String a = "hello";
//        String b = "world"; // 字符串拼接不会加额外的空格
//        System.out.println(a + b); // sout会自动输出一个换行 helloworld
        // 2、把多个字符串拼接在一起并用一个分割符分开 静态方法String.join("分割符", a, b, 拼接字符串)
//        String a = "hello";
//        String b = "world";
//        System.out.println(String.join("/", a, b)); // hello/world
        // 3、不可变字符串 无法直接修改字符的值
//        String a = "sdgf fdsfsdf";
//        for (int i = 0; i < a.length(); i++) {
//            System.out.println(a.charAt(i)); // 对象方法,charAt(int i)返回第i个代码单元的char,不可修改
//        }
        // 4、字符串的相等的判断
        // 字符串的相等的判断用 字符串对象方法str.equals(str1) 返回值为true or false
        // equals判断的是 字符值 是否相等;== 判断的是两个对象的地址是否相等
        // 对象方法str.compareTo(str1)==0 也可以盘点字符串是否相等
        // compareTo() 函数返回的是第一个不同的char的差值 this.value[i] - o.value[i];否则返回len1 - len2
//        String a = "abc";
//        String b = "abc";
//        String c = new String("abc");
//
//        System.out.println(a == b); // true 都指向常量池中
//        System.out.println(a == c); // false
//
//        System.out.println(a.equals(b)); // true
//        System.out.println(a.equals(c)); // true
        // 用char数组初始化String
//        char []aa = {'a', 'b', 'c'};
//        String a = new String(aa);
//        System.out.println(a);
        // 对象方法str.compareTo(str1)==0 也可以盘点字符串是否相等
//        String a = new String("abc");
//        String b = new  String("abc");
//        String c = new String("b");
//        String d = new String("a");
        // compareTo() 函数返回的是第一个不同的char的差值 this.value[i] - o.value[i];否则返回len1 - len2
//        System.out.println(a.compareTo(b)); // 0
//        System.out.println(a.compareTo(c)); // -1
//        System.out.println(a.compareTo(d)); // 2

//        String a = new String("abc");
//        String b = new  String("abcd");
//        System.out.println(a.compareTo(b)); // -1

        // 6、空串与Null串
        // 空串:长度为0的字符串,空串是一个Java对象
        // String变量还可以存放一个特殊的值 null,null串调用方法会引起空指针异常
        // 首先,要判断一个字符串是否为null , 再调用方法
//        String a = null;
//        a.length(); // java.lang.NullPointerException
        // 同时判断是否为null 是否为空字符串
//        if (str != null && str.lengthO != 0)

        // 7、构建字符串
        // StringBuilder字符串构建器类,用于频繁的连接字符串; 比用+连接快
        /*
        StringBuilder 与 StringBuffer 的 异同:
        API是相同的,可变字符串
        和 String 类不同的是,StringBuffer 和 StringBuilder 类的对象能够被多次的修改,并且不产生新的未使用对象。

        StringBuilder效率更高一些,确定单线程的情况下可以用它。不是线程安全的
        StringBuffer效率低一些,但允许多线程的方式操作执行添加或删除字符的操作。线程安全的
         */
//        StringBuilder builder = new StringBuilder();
//        builder.append('a');
//        builder.append("123");
//        String res = builder.toString();
//        System.out.println(res); // a123

        // 8、输入和输出
        // Scanner in = new Scanner(System.in);String a = in.nextLine();
        // 输入
//        Scanner in = new Scanner(System.in); // 用给定的输入流创建一个Scanner对象
//        String a = in.nextLine(); // 读取 下一行 的内容 ,返回值String类型
//        System.out.println(a);
//        int b = in.nextInt(); // 读取 下一个整数,返回值int
//        System.out.println(b);
        // 格式化输出
//        System.out.println(0.1 + 0.2); // 0.30000000000000004
//        System.out.printf("%tc", new Date()); // 星期五 十月 01 17:19:27 CST 2021
//        System.out.printf("%1$s %2$tB %2$te, %2$tY","Due data:", new Date()); // Due data: 十月 1, 2021
        // 参考索引值从1开始,而不是从0开始,%1$ 表示对第一个参数格式化。

        // 文件的输入和输出  必须try catch 捕获异常
//        Scanner in = new Scanner(Paths.get("myfile.txt"), "UTF-8"); //Error:(93, 22) java: 未报告的异常错误java.io.IOException; 必须对其进行捕获或声明以便抛出

    }
}

posted @ 2021-10-01 17:45  永恒&  阅读(23)  评论(0编辑  收藏  举报