今天看啥  ›  专栏  ›  独饮敌敌畏丶_4109

Java项目实战开发Day12 2020-04-03

独饮敌敌畏丶_4109  · 简书  ·  · 2020-04-03 16:15

内容

1.字符串String
2.StringBuilder
3.自动装箱
4.Date
5.Calendar

一.字符串String

详细请参看之前一篇博客: https://www.jianshu.com/p/abad41a3a481

1.主要特点:

String 是不可变的字符串。被final修饰,不能子类化


JDK帮助文档对String描述

2.字符串的几种声明方式

        //1.最常用的
        String country = "中国";
        String city1 = "重庆";
        //中国 存在常量区
        
        //2.使用构造方法创建
        String city = new String("重庆");
        //city变量在栈上定义,new的对象在堆里面

        //3.使用字符数组创建
        char[] name = new char[] {'a','b','c'};//abc
        String names = new String(name);
        System.out.println(names);

city和city1的本质区别


3.String中一些常用方法使用示例

首先是比较大小的一些方法

        System.out.println(city1 == city);//比较对象本身,也就是内存地址

        System.out.println(city.equals(city1));//比较内容,上面的输出false,这个输出true    
    
        System.out.println(city.compareTo("重庆"));//0
        System.out.println(city.compareTo("重庆1"));//-1
        System.out.println(city.compareTo("重"));//1
        //a > b 返回1 。a < b 返回-1 。a = b 返回0

还有一些其他的常用的方法,具体在下面代码

        //获取字符串的长度
        System.out.println(city.length());//输出2
        
        //获取i对应的字符
        System.out.println(city.charAt(0));//重
        
        //判断字符串是否为空
        System.out.println(city.isEmpty());//false
        //或者
        if(city.length() == 0) {
            System.out.println("字符串为空");
        }
        
        //判断某个字符串是否以某个字符串开头
        System.out.println(city.startsWith("重"));//true
        //从index以后有没有以“”开头的字符串,默认把第index个设为0
        System.out.println(city.startsWith("重", 0));//true
        
        //判断某个字符串是否以某个字符串结尾
        System.out.println(city.endsWith("庆"));//true
        
        //获取一个对象的哈希值
        System.out.println(city.hashCode());//1181273
        
        //输出索引值
        System.out.println(city.indexOf("重"));//0
        String longCity = "重庆很热";
        //跳过index开始的索引,也就是把index设为0
        System.out.println(longCity.indexOf("庆",2));//-1
        
        //获取子字符串
        System.out.println(city.substring(1));//从1到结尾//庆
        System.out.println(longCity.substring(1, 3));//庆很,第4个不要
        
        //字符串的替换
        System.out.println(city.replaceAll("重", "大"));//大庆
        System.out.println(city);//重庆,所以替换是弄了一个新的对象,原来的对象没有变化
        
        //字符串分割
        System.out.println(city.split("重"));//[Ljava.lang.String;@15db9742,直接输出哈希值了
        
        String longThing = "/我是/最/帅的。";
        String[] split = longThing.split("/");
        for(String comp:split) {
            System.out.println(comp + " ");
        }
        //输出:
        //我是 
        //最 
        //帅的。 

        //将字符串末尾的空格去掉
        String kongGe = "这些字后面是空格    ";
        System.out.print(kongGe.trim());
        System.out.print("----------");//这些字后面是空格----------
        System.out.print(kongGe);
        System.out.print("----------");//这些字后面是空格    ----------
        
        //字符串拼接 用加号即可
        System.out.println(1+"+"+2);//1+2
    

二.StringBuilder

也可参看: https://www.jianshu.com/p/67ee107a7735

StringBuilder中常用方法

        //之前的替换都是新建了一个对象,原来的对象没有发生变化。但是下面这个就发生变化了
        StringBuilder test = new StringBuilder();

        //往后加
        test.append("我是敌敌畏");
        System.out.println(test);//我是敌敌畏

        //替换:从哪到哪然后用什么替换
        test.replace(0,5,"我不是敌敌畏");//第5个(虽然这里没有)不进行替换
        System.out.println(test);//我不是敌敌畏

        //删除
        test.delete(0,1);
        System.out.println(test);//不是敌敌畏

        //插入
        test.insert(0, "我");
        System.out.println(test);//我不是敌敌畏

        //删除所有内容
        test.delete(0, test.length());//注意,不能用 test.length() - 1,因为后面那个参数的索引值不删
        System.out.println(test);//啥都不输出

三.自动装箱与自动拆箱

详细请参看: https://www.jianshu.com/p/67ee107a7735

四.Date

详细请参看: https://www.jianshu.com/p/683e37c6e0cc

使用示例

        //Date时间
        Date date = new Date();
        
        //格式化时间
        SimpleDateFormat temp = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss SSS");
        
        //输出格式化时间
        System.out.println(temp.format(date));
        //2020-04-03 15-40-21 323

五.Calendar

详细请参看: https://www.jianshu.com/p/683e37c6e0cc

1.注意点

Calendar不能直接new,而是要 getInstance()

2.使用示例

        Calendar calendar = Calendar.getInstance();
        
        //获取时间
        System.out.println(calendar.get(Calendar.YEAR));
        System.out.println(calendar.get(Calendar.MONTH) + 1);//注意,MONTH要加1
        System.out.println(calendar.get(Calendar.DATE));
        System.out.println(calendar.get(Calendar.HOUR));
        System.out.println(calendar.get(Calendar.MINUTE));
        System.out.println(calendar.get(Calendar.SECOND));
                
        /*
         * 2020
            4
            3
            3
            46
            44
         */

总结

今天绝大部分内容也都学过,更多的感受是,再一次接触相同知识时,理解更透彻了,之前虽然学过但也是模模糊糊,这次弄清楚了很多的点。另外,扑克牌程序还没有学好,我打算再写一篇博客专门研究这个程序。




原文地址:访问原文地址
快照地址: 访问文章快照