2020-08-04

常见对象

正则表达式

正则表达式

  • 正确规则的表达式 规则java给我们定的

    是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串。其实就是一种规则。有自己特殊的应用

  • 常规写法定义QQ号码格式(5-15位数字,首位不能为0)

import java.util.Scanner;
public class MyTest {
    public static void main(String[] args) {
        //常规写法定义QQ号码格式
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入QQ号码");
        String s = sc.nextLine();
        //定义一个标记
        boolean flag=false;
        //判断字符串长度以及首位
        if(s.length()>=5&&s.length()<=15&&!s.startsWith("0")){
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                //判断是否是纯数字
                if(!Character.isDigit(c)){
                    flag=false;
                    break;
                }else{
                    flag=true;
                }
            }
        }else{
            flag=false;
        }
        if(flag){
            System.out.println("QQ号码格式正确");
        }else{
            System.out.println("QQ号码格式不正确");
        }
    }
}
  • 正则表达式定义QQ号码规则(5-15位数字,首位不能为0)
import java.util.Scanner;
public class MyTest {
    public static void main(String[] args) {
        //正则表达式定义QQ号码规则
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入QQ号码");
        String s = sc.nextLine();
        //首位:1-9;其他位:0-9;其他位个数:4-14位
        String regx="[1-9][0-9]{4,14}";
        //matches():判断是否满足正则表达式要求
        boolean b = s.matches(regx);
        if(b){
            System.out.println("QQ号码格式正确");
        }else{
            System.out.println("QQ号码格式不正确");
        }
    }
}
正则表达式的组成规则

规则字符在java.util.regex Pattern类中

  • 字符

    x  字符x。举例:'a'表示字符a
    \\  反斜线字符
    \n  新行(换行)符('\u000A')
    \r  回车符('\u000D')
    
  • 字符类

    [abc]  a、b或c(简单类)
    [^abc]  任何字符,除了a、b或c(否定)
    [a-zA-Z]  a到z 或 A到Z ,两头的字母包括在内(范围)
    [0-9]  0到9的字符都包括
    
  • 预定义字符类

    .  任何字符。我的就是.字符本身,怎么表示呢? \.
    \d  数字:[0-9]
    \w  单词字符:[a-zA-Z_0-9]  在正则表达式里面组成单词的东西必须有这些东西组成
    \s  匹配空格字符
    
  • 边界匹配器

    ^  行的开头
    $  行的结尾
    \b  单词边界  就是不是单词字符的地方  
    
  • Dreedy数量词

    X? X  一次或一次也没有 比如""空串 就是没有
    X* X  零次或多次  大于等于1次 都算多次
    X+ X  一次或多次
    X{n} X  恰好 n 次 
    X{n,} X  至少 n 次 
    X{n,m} X  至少 n 次,但是不超过 m 次 
    
正则表达式的判断功能

正则表达式的判断功能

String类的功能:public boolean matches(String regex)

  • 判断手机号码是否满足规则(11位,13、15、17、18开头)
import java.util.Scanner;
public class MyTest {
    public static void main(String[] args) {
        //判断手机号码是否满足规则
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入手机号码");
        String s = sc.nextLine();
        String regx="[1][3,5,7,8][0-9]{9}";
        boolean b = s.matches(regx);
        if(b){
            System.out.println("手机号码格式正确");
        }else{
            System.out.println("手机号码格式不正确");
        }
    }
}
  • 校验用户邮箱是否满足要求(字母打头,6-18位,可以使用数字字母和下划线,@163.com结尾)
import java.util.Scanner;
public class MyTest {
    public static void main(String[] args) {
        //校验用户邮箱是否满足要求
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入邮箱");
        String s = sc.nextLine();
        String regx="[a-zA-Z][0-9a-zA-Z_]{6,18}@163\\.com";
        boolean b = s.matches(regx);
        if(b){
            System.out.println("邮箱格式正确");
        }else{
            System.out.println("邮箱格式不正确");
        }
    }
}
正则表达式的分割功能

正则表达式的分割功能 split()方法

String类的功能:public String[ ] split(String regex)

public class MyTest4 {
    public static void main(String[] args) {
        //根据正则来切割字符串,返回的是一个字符串数组
        String names="鹿秀儿=秀儿鹿=儿秀鹿=秀鹿儿";
        String[] split = names.split("=");
        System.out.println(split[0]);//鹿秀儿
        System.out.println(split[1]);//秀儿鹿
        System.out.println(split[2]);//儿秀鹿
        System.out.println(split[3]);//秀鹿儿
    }
}
把给定字符串中的数字排序

需求:字符串:”91 27 46 38 50”,写代码实现最终输出结果是:”27 38 46 50 91”

  • 分析:

  • a: 定义目标字符串"91 27 46 38 50"

  • b: 对这个字符串进行切割,得到的就是一个字符串数组

  • c::把b中的字符串数组转换成int类型的数组

    (1): 定义一个int类型的数组,数组的长度就是字符串数组长度

    (2): 遍历字符串数组,获取每一个元素,将其转换成int类型的数据

    (3):把int类型的数据添加到int类型的数组的指定位置

  • d:排序

  • e:创建一个StringBuilder对象,用来记录拼接的结果

  • f:遍历int类型的数组,,将其每一个元素添加到StringBuilder对象中

  • g:就是把StringBuilder转换成String

  • h: 输出

import java.util.Arrays;
public class MyTest {
    public static void main(String[] args) {
        //给定字符串中的数字排序
        String x="91 27 46 38 50";
        String[] strs = x.split("\\s+");
        System.out.println(Arrays.toString(strs));//[91, 27, 46, 38, 50]
        int[] ints = new int[strs.length];
        for (int i = 0; i < strs.length; i++) {
            ints[i]=Integer.parseInt(strs[i]);
        }
        System.out.println(Arrays.toString(ints));//[91, 27, 46, 38, 50]
        Arrays.sort(ints);
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < ints.length; i++)
            if (i == ints.length - 1) {
                s.append(ints[i]);
            }else{
                s.append(ints[i]+"\t");//27	38	46	50	91
            }
        System.out.println(s.toString());
    }
}
正则表达式的替换功能

正则表达式的替换功能

String类的功能:public String replaceAll(String regex,String replacement)

public class MyTest {
    public static void main(String[] args) {
        //正则表达式的替换功能
        String s="hello123world456hi789";
        String s1 = s.replaceAll("[^a-z]+","*");
        System.out.println(s1);//hello*world*hi*
    }
}
Pattern和Matcher的概述

正则的获取功能需要使用的类

public class MyTest {
    public static void main(String[] args) {
        //匹配器Matche   模式器Pattern
        //把一个正则表达式封装到模式器里面
        Pattern p = Pattern.compile("a*b");
        //调用模式器中的matcher("aaaaab");方法,传入一个待匹配的数据,返回一个匹配器
        Matcher m = p.matcher("aaaaab");
        //调用匹配器中的匹配的方法,看数据是否匹配正则
        boolean b = m.matches();
        System.out.println(b);
        //如果你的需求只是看一个数据符不符合一个正则,你只需要调用,String类中的matches("a*b")
        System.out.println("aaaaab".matches("a*b"));

        //之所以Java给我提供了  匹配器Matche   模式器Pattern 这两个类,是因为,这两个类中有更丰富的方法供我们使用
    }
}

典型的调用顺序

Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaaab");
boolean b = m.matches();
正则表达式的获取功能

正则表达式的获取功能

Pattern和Matcher的结合使用

public class MyTest {
    public static void main(String[] args) {
        //获取下面字符串中 是三个字母组成的单词。
       // da jia ting wo shuo, jin tian yao xia yu, bu shang wan zi xi, gao xing bu?

        String str="da jia ting wo shuo, jin tian yao xia yu, bu shang wan zi xi, gao xing bu?";

       String regx="\\b[a-z]{3}\\b";

       Pattern pattern = Pattern.compile(regx);
        Matcher matcher = pattern.matcher(str);

        while (matcher.find()) {
            String s = matcher.group();
            System.out.println(s);
        }
    }
}

Math类

Math类概述和方法使用

Math类概述

  • Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数

成员变量

  • public static final double E:自然底数
  • public static final double PI:圆周率

成员方法

  • public static int abs(int a):取绝对值
  • public static double ceil(double a):向上取整
  • public static double floor(double a):向下取整
  • public static int max(int a,int b):获取最大值
  • public static int min(int a,,int b):获取最小值
  • public static double pow(double a,double b):获取a的b次幂
  • public static double random():获取随机数,返回带正号的 double 值,该值大于等于 0.0 且小于 1.0
  • public static int round(float a):四舍五入
  • public static double sqrt(double a):获取正平方根

Random类

Random类的概述和方法使用

Random类的概述

  • 此类用于产生随机数如果用相同的种子创建两个 Random 实例
  • 则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列

构造方法

  • public Random():没有给定种子,使用的是默认的(当前系统的毫秒值)
  • public Random(long seed):给定一个long类型的种子,给定以后每一次生成的随机数是相同的

成员方法

  • public int nextInt():没有参数,表示的随机数范围,是int类型的范围
  • public int nextInt(int n):可以指定一个随机数范围
  • public void nextBytes(byte[ ] bytes):生成随机字节并将其置于用户提供的空的 byte 数组中

System类

System类的概述和方法使用

System类的概述

  • System 类包含一些有用的类字段和方法。它不能被实例化

成员方法

  • public static void gc():调用垃圾回收器
  • public static void exit(int status):退出java虚拟机。0 为正常退出;非0为 异常退出
  • public static long currentTimeMillis():获取当前时间的毫秒值

BigDecimal类

BigDecimal类的概述和方法使用

BigDecimal的概述

  • 由于在运算的时候,float类型和double很容易丢失精度
  • 所以,为了能精确的表示、计算浮点数,Java提供了BigDecimal
  • 不可变的、任意精度的有符号十进制数

构造方法

  • public BigDecimal(String val)

成员方法

  • public BigDecimal add(BigDecimal augend):加
  • public BigDecimal subtract(BigDecimal subtrahend):减
  • public BigDecimal multiply(BigDecimal multiplicand):乘
  • public BigDecimal divide(BigDecimal divisor):除
  • public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode):scale 小数点后面保留几位
  • roundingMode 取舍模式 比如四舍五入

Date类

Date类的概述和方法使用

Date类的概述

  • 类 Date 表示特定的瞬间,精确到毫秒

构造方法

  • public Date()
  • public Date(long date):把一个long类型的毫秒值转换成一个日期对象

成员方法

  • public long getTime():获取一个日期对象对象毫秒值
  • public void setTime(long time):给一个日期对象设置上指定的毫秒值 例:date.setTime(1000 * 60 * 60) ;
public class MyTest {
    public static void main(String[] args) {
        //Date-----long
        Date date = new Date();
        long time = date.getTime();


       // long-----Date
        //方式1
        Date date1 = new Date(1000 * 60);

        Date date2 = new Date();
        date2.setTime(200000);
    }
}
SimpleDateFormat类实现日期和字符串的相互转换

SimpleDateFormat

  • 可以把一个日期对象格式化成一个文本(字符串) , 也可以把一个日期字符串解析成一个日期对象

构造方法

  • public SimpleDateFormat():使用默认的模式来创建一个SimpleDateFormat对象
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyTest {
    public static void main(String[] args) {
        //把一个日期对象格式化成一个文本(字符串)
        //Date----String format(date);
        Date date = new Date();
        System.out.println(date);
        //格式化日期的一个类,可以按照我们自己喜欢的日期格式,进行格式化
        //按照我们指定的 格式来进行格式化
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日-HH时mm分ss秒" );
        String format = simpleDateFormat.format(date);
        System.out.println(format);
    }
}
  • public SimpleDateFormat(String pattern):使用指定的模式(规则比如yyyy:MM:dd HH:mm:ss)来创建一个SimpleDateFormat对象
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyTest {
    public static void main(String[] args) throws ParseException {
        //把一个日期字符串解析成一个日期对象
        //String----Date parse(str);
        String str="2020-01-01 16:30:30";
        //注意:日期字符串和指定的格式要对应
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //把日期字符串,解析成日期对象。
        Date date = dateFormat.parse(str);
        System.out.println(date);
    }
}

规则的定义

  • y–年
  • M–月
  • d–日
  • H–时
  • m–分
  • s–秒

成员方法

  • public String format(Date date):把一个日期对象格式化成一个字符串
  • public Date parse(String dateStr):把一个日期字符串解析成一个日期对象,注意要以指定格式解析
你来到这个世界多少天案例

需求:算一下你来到这个世界多少天?

  • 分析:
  • a:键盘录入一个生日(日期字符串)
  • b:把这个日期字符串对象解析成一个日期对象
  • c:获取b中的日期对象对应的毫秒值
  • d:获取当前系统时间对应的毫秒值
  • e:使用d中的毫秒值 - c中的毫秒值
  • f: 把e中的差值换算成对应的天,差值/1000/60/60/24
  • g:输出
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Scanner;
public class MyTest {
    public static void main(String[] args) throws ParseException {
        //你来到这个世界多少天
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的出生年月日(xxxx-xx-xx)");
        String birthday = sc.nextLine();
        long time = new SimpleDateFormat("yyyy-MM-dd").parse(birthday).getTime();
        long now = System.currentTimeMillis();
        System.out.println((now - time) / 1000 / 60 / 60 / 24);
    }
}

Calendar类

Calendar类的概述和获取日期的方法

Calendar类的概述

  • Calendar 类是一个抽象类,不能直接new对象,可以通过他的一个静态成员方法getInstance()来获取他的对象
  • 它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR
  • 等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法

成员方法

  • public static Calendar getInstance():使用默认时区和语言环境获得一个日历对象
  • public int get(int field):获得给定日历字段对应的值 field通过Calendar提供的字段来拿
public class MyTest {
    public static void main(String[] args) {
        Calendar now = Calendar.getInstance();
        int year = now.get(Calendar.YEAR);
        int month = now.get(Calendar.MONTH);
        int day = now.get(Calendar.DAY_OF_MONTH);
        int hour = now.get(Calendar.HOUR);
        int minute = now.get(Calendar.MINUTE);
        int second = now.get(Calendar.SECOND);
        System.out.println(year + " " + month + " " + day + " " + hour + " " + minute + " " + second);
    }
}
Calendar类的add()和set()方法

成员方法

  • public void add(int field,int amount):根据日历的规则,为给定的日历字段添加或减去指定的时间量
  • public final void set(int year,int month,int date):设置日历时间 年月日
import java.util.Calendar;
public class MyTest {
    public static void main(String[] args) {
        //获取当前日历
        Calendar instance = Calendar.getInstance();
        //为给定的日历字段(Calendar.YEAR)添加或减去指定的时间量(3),减去写负号
        instance.add(Calendar.YEAR,3);
        int year=instance.get(Calendar.YEAR);
        System.out.println(year);
    }
}
import java.util.Calendar;
public class MyTest {
    public static void main(String[] args) {
        Calendar instance = Calendar.getInstance();
        //设置日历时间 年月日
        instance.set(2222, 6, 9);
        int year=instance.get(Calendar.YEAR);
        int month=instance.get(Calendar.MONTH);
        int day=instance.get(Calendar.DAY_OF_MONTH);
        System.out.println(year + "年" + month + "月" + day+"日");
    }
}
如何获取任意年份的2月份有多少天

需求:键盘录入任意一个年份,获取任意一年的二月有多少天

  • 分析:
  • a:键盘录入一个年份
  • b:创建一个Calendar对象
  • c:把这个Calendar的时间设置为a中录入的年的3月1号;注意2表示3月
  • d:向前推算一天
  • e:获取月中的天
  • f:输出
import java.util.Calendar;
import java.util.Scanner;
public class MyTest {
    public static void main(String[] args) {
        //键盘录入一个年份
        Scanner sc = new Scanner(System.in);
        System.out.println("输入一个年份");
        int i = sc.nextInt();
        //创建一个Calendar对象
        Calendar instance = Calendar.getInstance();
        //把这个Calendar的时间设置为a中录入的年的3月1号
        instance.set(i, 2, 1);
        //向前推算一天
        instance.add(Calendar.DAY_OF_MONTH, -1);
        //获取月中的天
        int day=instance.get(Calendar.DAY_OF_MONTH);
        System.out.println("2月有"+day+"天");
        //补充:判断是不是一个闰年
    if(year%4==0&&year%100!=0||year%400==0){
           System.out.println("是闰年");
    }
    }
}

补充BigInteger

可以让超过long范围内的数据进行运算

构造方法

  • public BigInteger(String val)

成员方法

  • public BigInteger add(BigInteger val)
  • public BigInteger subtract(BigInteger val)
  • public BigInteger multiply(BigInteger val)
  • public BigInteger divide(BigInteger val)
  • public BigInteger[ ] divideAndRemainder(BigInteger val)

示例

BigInteger bi1=new BigInteger("100");
 BigInteger bi2=new BigInteger("2");
        
System.out.println(bi1.add(bi2));   //+
System.out.println(bi1.subtract(bi2));   //-
System.out.println(bi1.multiply(bi2));   //*
System.out.println(bi1.divide(bi2));    //(除)
        
BigInteger[] arr=bi1.divideAndRemainder(bi2);    //取除数和余数
        for(int i=0;i<arr.length;i++){
            System.out.println(arr[i]);
        }

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值