day11_java API String类,StringBuffer类,正则表达式,包装类拆箱和装箱

API

String

创建方法不同,存储方式不同

public class TestString {

    public static void main(String[] args) {
        //创建对象方法一,在常量池中,相同值的地址是相同的
        String s1 = "hello";
        String s4 = "hello";
        String s5 = "abc";

        //创建对象方法二,在堆里
        String s2 = new String();//创建一个空串
        String s3 = new String("hello");


        System.out.println(s1 == s3);

        System.out.println(s1 == s4);
    }

}

String类常用方法

//--------------------------------------------------------
        //String 不可变类
        String s = "hello";
        //连接字符串
        s.concat("tom");
        //s对应的对象不能改,还是hello
        System.out.println(s);

        //需要创建新的对象,引用指向新的对象
        s= s.concat("tom");
        System.out.println(s);

        //--------------------------------------------------------
        //字符串的长度也就是字符的个数,hellotom,
        System.out.println(s.length());
        //--------------------------------------------------------
        //两个字符串的值是否相等
        String str1 = "hello";
        String str2 = "hello";
        String str3 = new String("hello");

        System.out.println(str1.equals(str2));//true
        System.out.println(str1.equals(str3));//true
        //区分大小写
        System.out.println(str1.equals("Hello"));//false

        //--------------------------------------------------------
        //忽略大小写
        System.out.println(str1.equalsIgnoreCase("Hello"));//true
        //转为大写
        System.out.println("HELLo".toUpperCase());//HELLO
        //转为小写
        System.out.println("HELLo".toLowerCase());//hello

        //--------------------------------------------------------
        //索引从0开始
        String sg = "hellohello";
        //参数字符串在原字符串对象中第一次出现的位置索引
        System.out.println(sg.indexOf('e'));//1
        System.out.println(sg.indexOf("he"));//0
        //不存在返回-1
        System.out.println(sg.indexOf("abc"));//-1
        System.out.println(sg.lastIndexOf("he"));//5

        //--------------------------------------------------------

        String sg1 = "hellohello";
        //返回参数位置对应的字符char
        System.out.println(sg1.charAt(1));//e


        String sg2 = "helloabc";
        //取子串,从参数位置取到末尾
        System.out.println(sg2.substring(5));//abc

        //取子串,从第一个参数位置开始取到第二个参数的前一位【);
        System.out.println(sg2.substring(0,5));//hello

        //--------------------------------------------------------
        //去除首尾空格
        String sg3 = "        h     e   l     l     o       ";
        System.out.println(sg3.trim());//h     e   l     l     o


        //字符串替换(旧的字符串,新的字符串)
        String sg4 = "helloabc";
        System.out.println(sg4.replace("hello", "你好"));//你好abc
        //去掉字符串中所有的空格,用空串替换空格
        System.out.println(sg3.replace(" ", ""));//hello

        //--------------------------------------------------------
        //判断是不是java文件
        String  sg5 = "abc.java";
        //是否以指定字符串结尾
        System.out.println(sg5.endsWith("java"));//true
        //是否以指定字符串开头
        System.out.println(sg5.endsWith("abc"));//true

        //--------------------------------------------------------
        //比较字符串大小
        String sg6 = "hello";
        String sg7 = "abc";
        //比较对象在比较的参数之前,返回负数,之后返回正数,相等返回0
        System.out.println(sg6.compareTo(sg7));//7
        System.out.println("xxx".compareTo("aaa"));//23
        System.out.println("xxx".compareTo("xxx"));//0


        //--------------------------------------------------------
        //是否包含指定参数的字符串
        System.out.println("hello".contains("ello"));//true

        //把字符串转换成字符数组
        char [] c = "hello".toCharArray();
        System.out.println(Arrays.toString(c));//[h, e, l, l, o]

        //分割,用参数分隔成数组
        String  [] strs = "aa bb cc dd ee ff".split(" ");
        System.out.println(strs.length);
        //遍历输出验证
        for(String ss : strs){
            System.out.println(ss);
        }




    }

}

StringBuffer类的方法

可变类

在字符串的值频繁更改时,用StringBuffer可以提高效率

package day18;

import java.util.Scanner;

public class TestStringBuffer {

    public static void main(String[] args) {
        StringBuffer sf = new StringBuffer();//空串16个字符大小的缓冲区
        System.out.println(sf.capacity());
        //超16会扩容,大约成倍
        sf.append("hellllllllllllllllllllllllllllllllllllllllllllllo00000000000000000000000000000000000000000000000000000");
        System.out.println(sf.capacity());

        StringBuffer sf1 = new StringBuffer();//空串16个字符大小的缓冲区
        System.out.println(sf1.capacity());//16
        sf1.append("hell0");
        //缩小缓冲数组大小
        sf1.trimToSize();
        System.out.println(sf1.capacity());//5

        //可以自己设置缓冲数组容量的大小
        StringBuffer sf2 = new StringBuffer(100);//给出最大值容量
        System.out.println(sf2.capacity());//100

        //append方法
        StringBuffer sr = new StringBuffer("hello");//给出最大值容量
        sr.append("tom");
        System.out.println(sr);//hellotom
        char [] c ={'a','b','c'};
        //(字符数组,起始位置,几个字符)
        sr.append(c,1,2);
        System.out.println(sr);//hellotombc

        //-------------------------------------------
        //在指定的位置插入一个字符串
        sr.insert(5, "您好");
        System.out.println(sr);//hello您好tombc
        //修改某个位置的字符
        sr.setCharAt(5, '你');//
        System.out.println(sr);//hello你好tombc
        //删除指定位置的字符
        sr.deleteCharAt(5);
        System.out.println(sr);//hello好tombc
        //删除某一部分字符序列【起始位置,终止位置)
        sr.delete(6, 9);
        System.out.println(sr);//hello好bc
        //反转字符串
        sr.reverse();
        System.out.println(sr);//cb好olleh

        //转换为String类型
        String strr = sr.toString();

        //String 转为StringBuffer
        Scanner input = new Scanner(System.in);
        System.out.println("输入一行字符串");
        String str = input.next();
        StringBuffer sff = new StringBuffer(str);

    }


}












String ,StringBuffer,StringBuilder的区别:

  1. String 不可变字符串类: 表示一个字符串,用String
    字符串的值频繁修改,用String效率低,需要不断创建新的对象,那就选择用StringBuffer,StringBuilder

  2. StringBuffer,StringBuilder可变字符串,StringBuffer时安全的,速度慢;StringBuilder非安全,速度快

正则表达式

用某种模式去匹配指定字符串的一种方式

优点:

  1. 简介的代码
  2. 严谨地验证文本框的内容

符号

符号 描述
\D 除了数字之外的任何字符,等价于[^0-9]
\d 匹配一个数字字符,等价于[0-9]
\W 任何非单词字符,等价于[^A-Za-z0-9_]
\w 任何单词字符,等价于[A-Za-z0-9_]
. 除了换行以外的任意字符
符号 描述
{n} 匹配前一项n次
{n,m} 匹配前一项至少n次,但是不能超过m次
* 匹配前一项0次或多次,等价于{0,}
+ 匹配前一项1次或多次,等价于{1,}
? 匹配前一项0次或1次,也就是说前一项是可选的,等价于{0,1

语法

  • 定义正则表达式: Pattern.compile(regString,);
  • 表达式的模式:Matcher matcher = ptn.matcher(“需要匹 配的数据”);
  • 验证:matcher.matches()
package day18;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**正则表达式 */

public class Ex4 {

    public static void main(String[] args) {
        //1.设置正则表达式
//      Pattern p = Pattern.compile("[0-9]{6}");
//      Pattern p = Pattern.compile("\\d{6}");
//      //2.设置要验证的数据
//      Matcher m = p.matcher("123456");
//      //3.验证
//      System.out.println(m.matches());



        Pattern p = Pattern.compile("1\\d{10}");
        Scanner input = new Scanner(System.in);
        System.out.println("请输入十一位的电话号码");
        String tel = input.next();
        Matcher m = p.matcher(tel);
        System.out.println(m.matches());

        System.out.println("请输入密码:");
        String passwd = input.next();
        Pattern p1 = Pattern.compile("\\w{4,10}");
        Matcher m1 = p1.matcher(passwd);
        System.out.println(m1.matches());


        System.out.println("请输入用户名");
        String name = input.next();
        Pattern p2 = Pattern.compile("[a-zA-z][a-zA-Z0-9]{3,15}");
        Matcher m2 = p2.matcher(name);
        System.out.println(m2.matches());


    }

}

包装类拆箱,装箱

JDK提供了对所有基本数据类型的包装类

  • byte ——-> Byte 字节包装类
  • char ——-> Character 字符包装类
  • short ——-> Short 短整型包装类
  • int ——-> Integer 整型包装类
  • long ——-> Long 长整型包装类
  • double ——-> Double 双精度包装类
  • float ——-> Float 单精度包装类
  • boolean ——-> Boolean 布尔类型包装类

装箱:基本数据类型包装成对象;基本数据类型 -》类类型

拆箱:把对象中的值取出;类类型 -》基本类型

Date类

  • API
    文档中的大部分方法均已过时,不建议使用
  • 日期格式应该使用SimpleDateFormat类;

- 提取时间分量的方法应该使用Calendar类

package day18;

import java.security.Timestamp;
import java.sql.Date;
import java.sql.Time;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Calendar;

public class TestDate {

    public static void main(String[] args) {
        //-----------------------父类java.util--------------------------
        java.util.Date date = new java.util.Date();
        System.out.println(date);

        System.out.println(date.getTime());
//      System.out.println(System.currentTimeMillis());
        //记录当时的瞬间
        java.util.Date date1 = new java.util.Date(1526978531991L);
        System.out.println(date1);
        //------------------子类java.sql------------------------------
        Date d1 = new Date(date.getTime());
        //将日期转换为字符串
        String sd1 = d1.toString();
        System.out.println(d1);//2018-05-22
        System.out.println(sd1);//
        //将字符串转换为date类型
        d1 = Date.valueOf(sd1);

        Time t1 = new Time(date.getTime());
        System.out.println(t1);
//      Timestamp tp1 = new Timestamp(date.getTime());
//      System.out.println(tp1);

        //--------------------格式化--------------------------
        //数字
        //NumberFormat
        NumberFormat nf1 = NumberFormat.getInstance();//当前缺省的数字格式化方式进行格式化
        System.out.println(nf1.format(12.234455));

        nf1 = NumberFormat.getCurrencyInstance();//当前缺省的货币格式化方法
        System.out.println(nf1.format(24.45645));

        //子类DecimalFormat
        DecimalFormat df1 = new DecimalFormat("00.00");
        System.out.println(df1.format(4.45645));

        //日期格式化
        DateFormat f1 = DateFormat.getInstance();
        System.out.println(f1.format(date));
        DateFormat f2 = DateFormat.getDateInstance();//日期
        System.out.println(f2.format(date));

        f1 = DateFormat.getDateInstance(DateFormat.SHORT);//短日期形式
        System.out.println(f1.format(date));

        f1 = DateFormat.getDateInstance(DateFormat.MEDIUM);//中日期形式
        System.out.println(f1.format(date));

        f1 = DateFormat.getDateInstance(DateFormat.LONG);//长日期形式
        System.out.println(f1.format(date));


        f1 = DateFormat.getDateInstance(DateFormat.FULL);//完整日期形式
        System.out.println(f1.format(date));


        //时间实例
        f1 = DateFormat.getTimeInstance();
        System.out.println(f1.format(date));

        //日期时间实例
        f1= DateFormat.getDateTimeInstance();
        System.out.println(f1.format(date));

        f1= DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL);
        System.out.println(f1.format(date));

        //----------------------------------------------------
        SimpleDateFormat stf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
        System.out.println(stf1.format(date));

        Calendar c = Calendar.getInstance();
        System.out.println(c.get(Calendar.YEAR));//2018
        System.out.println(c.get(Calendar.MONTH)+1);//5
        System.out.println(c.get(Calendar.DATE));//23

        Calendar c1 = Calendar.getInstance();
        c1.set(2012, 3,3);
        System.out.println(c1.getTime());
        System.out.println(stf1.format(c1.getTime()));
        c1.add(Calendar.YEAR, 10);
        System.out.println(stf1.format(c1.getTime()));//2022-04-03 09:41:03.286

        Calendar c2 = Calendar.getInstance();
        c2.set(2023, 5,1);

        System.out.println(c1.after(c2)); //false

        //----------------------jdk8.0  java.time---------------------------------------------
        //日期
        LocalDate ldate = LocalDate.now();
        System.out.println(ldate);

        LocalDate ldate1 = LocalDate.of(2018, 5, 6);
        System.out.println(ldate1);
        System.out.println(ldate1.getYear());//2018
        System.out.println(ldate1.getMonth());//MAY
        System.out.println(ldate1.getDayOfMonth());//6
        System.out.println(ldate1.getDayOfYear());//126
        //几年后
        System.out.println(ldate1.plusYears(2));//2020-05-06
        //几年前
        System.out.println(ldate1.minusYears(2));//2016-05-06


        //时间
        LocalTime ltime = LocalTime.now();
        System.out.println(ltime);  //09:51:33.313
        LocalTime ltime1 = LocalTime.of(11,22,33);
        System.out.println(ltime1.getHour());//11
        System.out.println(ltime.minusHours(2));//07:53:47.146


        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);
        System.out.println(ldt.getYear());
        System.out.println(ldt.getHour());
        System.out.println(ldt.plusHours(2));


        LocalDateTime ldt1 = LocalDateTime.now();
        LocalDateTime ldt2 = LocalDateTime.of(2012, 2,3,5,3,16);
        Duration d = Duration.between(ldt2, ldt1);
        System.out.println(d.toDays());  //2301
    }

}








猜你喜欢

转载自blog.csdn.net/qq_24135817/article/details/80561707