常用类,正则表达式

一、正则表达式

1.1、正则表达式概述及基本使用

正则表达式:是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串。其实就是一种规则。有自己特殊的应用。
例如:校验qq号码.
1:要求必须是5-15位数字
2:0不能开头

//普通实现方式
public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的qq号");
        String qq = scanner.nextLine();
        System.out.println(check(qq));
    }
    public static boolean check(String qq){
        boolean flag=true;
        if(qq.length()>=5&&qq.length()<=15){
            if(!qq.startsWith("0")){
                for (int i = 0; i < qq.length(); i++) {
                    char ch = qq.charAt(i);
                    if(!Character.isDigit(ch)){
                        flag=false;
                        break;
                    }
                }
            }else{
                flag=false;
            }
        }
        return flag;
    }
}
//正则表达式
public class Test2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的qq号");
        String qq = scanner.nextLine();
        System.out.println(check(qq));
    }
    public static boolean check(String qq){
        String regex="[1-9][0-9]{4,14}";
        boolean flag=qq.matches(regex);
        return flag;
    }
}

1.2、正则表达式的组成规则

规则字符在java.util.regex Pattern类中
	A:字符
		x 字符 x。举例:'a'表示字符a
		\\ 反斜线字符。
		\n 新行(换行)符 ('\u000A') 
		\r 回车符 ('\u000D')
	B:字符类
		[abc] a、b 或 c(简单类) 
		[^abc] 任何字符,除了 a、b 或 c(否定) 
		[a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围) 
		[0-9] 09的字符都包括
	C:预定义字符类
		. 任何字符。我的就是.字符本身,怎么表示呢? \.
		\d 数字:[0-9]
		\w 单词字符:[a-zA-Z_0-9]
			在正则表达式里面组成单词的东西必须有这些东西组成
		\s 匹配空格字符	
	D:边界匹配器
		^ 行的开头 
		$ 行的结尾 
		\b 单词边界
			就是不是单词字符的地方。
			举例:hello world?haha;xixi
	E:Greedy 数量词 
		X? X,一次或一次也没有 比如""空串 就是没有
		X* X,零次或多次  大于等于1次 都算多次
		X+ X,一次或多次
		X{n} X,恰好 n 次 
		X{n,} X,至少 n 次 
		X{n,m} X,至少 n 次,但是不超过 m 次 

1.3、正则表达式的应用

①判断功能(校验邮箱)

public class Test4 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的手机号");
        String str = scanner.nextLine();
        System.out.println(check(str));
    }

    private static boolean check(String str) {
        String regex="[a-zA-Z0-9_]+@[a-zA-Z0-9_]{2,6}\\.[a-zA-Z0-9_]{2,3}";
        boolean flag = str.matches(regex);
        return flag;

    }
}

②分割功能
有如下一个字符串:”91 27 46 38 50”,请写代码实现最终输出结果是:”27 38 46 50 91”

public class Test5 {
    public static void main(String[] args) {
        String str="91 27 46 38 50";
        String[] arrString = str.split(" ");
        int[] arrInt=new int[arrString.length];
        for (int i = 0; i < arrString.length; i++) {
            arrInt[i]=Integer.parseInt(arrString[i]);
        }
        Arrays.sort(arrInt);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arrInt.length; i++) {
            sb.append(arrInt[i]).append(" ");
        }
        String ss = sb.toString().trim();
        System.out.println(ss);
    }
}

③替换功能
论坛发表帖子,帖子中需要将数字替换为"*"

public class Test6 {
    public static void main(String[] args) {
        String str="hello123java456";
        String regex="\\d+";
        String ss = str.replaceAll(regex, "*");
        System.out.println(ss);
    }
}

④获取功能
正则的获取功能需要使用的类
A:Pattern和Matcher
B:模式和匹配器的典型调用顺序

	典型的调用顺序是 
	Pattern p = Pattern.compile("a*b");
	Matcher m = p.matcher("aaaaab");
	boolean b = m.matches();

需求:获取下面这个字符串中由三个字符组成的单词
da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?

public class Test7 {
    public static void main(String[] args) {
        String str="da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?";
        String regex="\\b\\w{3}\\b";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);
        while(m.find()){
            System.out.println(m.group());
        }
    }
}

注意:使用的是 find()方法 和 group()方法 注意一定要先使用find()方法先找到 才能用group()方法获取出来

二、Math类

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

成员方法

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 double pow(double a,double b)  //a的b次幂
public static double random()  //随机数[0.0,1.0)
public static int round(float a) //四舍五入
public static double sqrt(double a) //正平方根

三、Random类

3.1、Random类概述及其构造方法

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

构造方法:

public Random()            //没有给种子,用的是默认种子,当前时间的毫秒值
public Random(long seed)    //固定种子

3.2、Random类成员方法

public int nextInt()         //返回int范围内的随机数
public int nextInt(int n)        //返回[0,n)之间的随机数

给定种子后,每次得到的随机数是相同的

public static void main(String[] args) {
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            int num = random.nextInt(100);
            System.out.println(num);
        }
    }

四、System类概述及其成员方法

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

成员方法

public static void gc()       //运行垃圾回收器
public static void exit(int status)
public static long currentTimeMillis()
public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)

五、BigInteger类

5.1、BigInteger类概述及其构造方法

BigInteger类概述
可以让超过Integer范围内的数据进行运算
构造方法

public BigInteger(String val)

5.2、BigInteger类成员方法

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)

六、BigDecimal

6.1、BigDecimal类概述及其构造方法

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

public BigDecimal(String val)

6.2、BigDecimal类成员方法

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)

七、日期类

7.1、Date类概述及其方法

Date类概述
类 Date 表示特定的瞬间,精确到毫秒。
构造方法

public Date()
public Date(long date)
public static void main(String[] args) {
        Date date1 = new Date();
        System.out.println(date1);
        Date date2 = new Date(1000*60*60);
        System.out.println(date2);
    }

成员方法

public long getTime()
public void setTime(long time)
 public static void main(String[] args) {
        Date date = new Date();
        long time = date.getTime();
        System.out.println(time);
        date.setTime(1000);
        System.out.println(date);
    }

7.2、DateFormat类概述及其方法

DateFormat类概述
1、DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。
2、是抽象类,所以使用其子类SimpleDateFormat、SimpleDateFormat

构造方法:

public SimpleDateFormat()
public SimpleDateFormat(String pattern)

成员方法

public final String format(Date date)
public Date parse(String source)
public static void main(String[] args) {
        Date date = new Date();
        SimpleDateFormat s = new SimpleDateFormat();
        String str = s.format(date);
        System.out.println(str);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
        String str2 = sdf.format(date);
        System.out.println(str2);
    }

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

public static void main(String[] args) throws ParseException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的出生年月日");
        String str = scanner.nextLine();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(str);
        long mytime = date.getTime();
        long time = System.currentTimeMillis();
        long day=(time-mytime)/1000/60/60/24/365;
        System.out.println(day);

    }

7.3、Calendar类概述及其方法

Calendar类概述
Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。
成员方法:

public static Calendar getInstance()
public int get(int field)
public void add(int field,int amount)
public final void set(int year,int month,int date)
 public static void main(String[] args) {
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int date = c.get(Calendar.DATE);
        System.out.println(year+"年"+(month+1)+"月"+date+"日");
    }
public static void main(String[] args) {
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int date = c.get(Calendar.DATE);
        System.out.println(year+"年"+(month+1)+"月"+date+"日");
        c.add(Calendar.YEAR,-3);
         year = c.get(Calendar.YEAR);
         month = c.get(Calendar.MONTH);
         date = c.get(Calendar.DATE);
        System.out.println(year+"年"+(month+1)+"月"+date+"日");
        c.set(2020,05,06);
         year = c.get(Calendar.YEAR);
         month = c.get(Calendar.MONTH);
         date = c.get(Calendar.DATE);
        System.out.println(year+"年"+(month+1)+"月"+date+"日");
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值