正则表达式
public class Regex {
/*
正则表达式: 是一种模式匹配语言 用一个公式(规则 使用特定的符号来表示的) 去匹配一个字符串
使用场景:
输入验证手机号,邮箱....
学习正则表达式中的规则
.
|
\d [0-9]
\D 不是数字 [^0-9]
数量控制
[a-z]
[A-Z]
\s 空格
\w [a-zA-Z_0-9]
[\u4e00-\u9fa5]汉字
*/
public static void main(String[] args) {
// String s = "1";
// boolean res = s.matches("\\d");//0-9中的一个数
// System.out.println(res);//true
// String s = "12";
//boolean res = s.matches("\\d");//0-9中的一个数
//System.out.println(res);//false
//String s = "";
//boolean res = s.matches("[0-9]?");//? 一次或一次也没有
// System.out.println(res);//true
//String s = "123";
//boolean res = s.matches("[0-9]?");//? 一次或一次也没有
// System.out.println(res);//false
// String s = "1";
//boolean res = s.matches("\\d?");//? 一次或一次也没有
// System.out.println(res);//true [0-9]?和\\d?一样的意思
// String s = "1123";
//boolean res = s.matches("\\d*"); //0次或多次
//System.out.println(res);//true
//String s = "";
// boolean res = s.matches("\\d+");//一次或多次
//System.out.println(res);//false
// String s = "123456";
//boolean res = s.matches("\\d{6}");// 只能是6位数字
//System.out.println(res);//true
//String s = "1234567";
//boolean res = s.matches("\\d{6,}");//只能是数字并且大于等于6个
//System.out.println(res);//true
//String s = "123456789";
//boolean res = s.matches("\\d{6,8}");//只能是数字并且大于等于6个小于等8
// System.out.println(res);//false
//String s = "02345678";
//boolean res = s.matches("[1-9]{6,8}");//第一位是1-9,之后是6-8位
//System.out.println(res);//false
//String s = "abCdef";
//boolean res = s.matches("[a-zA-Z]{6,8}");//不区分大小写字母6-8个
// System.out.println(res);//true
// String s = "aCSFS9";
// boolean res = s.matches("\\w+");//A-Za-z0-9都行不限个数
//System.out.println(res);//true
//手机号 qq 邮箱 fewq6234r@qq.com.cn
//boolean res = "13355556662".matches("1[35789]\\d{9}");
//System.out.println(res);//true
//boolean res = "13355566662".matches("[1-9]\\d{5,11}");
// System.out.println(res);//true
//boolean res = "13355566662@qq.com.cn".matches("\\w{6,16}@\\w{2,6}\\.(com|com\\.cn)");
// System.out.println(res);//true
boolean res = "张三".matches("[\\u4e00-\\u9fa5]{2,5}");
System.out.println(res);//true
}
}
StringBuffer类和StringBuilder类
StringBuffer 值可变的字符串, 底层也是char[],没有被final修饰,是线程安全的
/* new StringBuffer();
super(16)//继承的字符串长度是16
char[] value = new char[16];
new StringBuffer("abc");
super(s.length+16)
char[] value = new char[16];
new StringBuffer(30);
char[] value = new char[30];
*/
//StringBuffer s = new StringBuffer();
StringBuffer s = new StringBuffer(8);
/*
s.append("def"); 向StringBuffer的末尾添加内容,
添加前会检测数组是否能够放的下内容,如果放不下,那么会创建一个新的数组
*/
s.append("def");//添加
s.append("def");
s.append("def");
//s.insert(0, "xxx");//向指定的位置上添加数据
//s.deleteCharAt(0);//删除第0个位置的元素
//s.delete(0,3);//删除0-3位置(不包括3)的元素
// s.replace(0, 3, "aaa");//代替0-3(不包括3)
// s.reverse();//字符串反转
String s1 = s.substring(2);//截取一个字符串副本返回
System.out.println(s1);
System.out.println(s);
/*
StringBuilder 也是值可变的字符串, 是线程不安全的,其他和StringBuffer一样
*/
StringBuilder t = new StringBuilder(8);
t.append("def");
System.out.println(t);
t.insert(0, "aaa");
System.out.println(t);
区别:String 值不可变的 适用于少量的字符串拼接操作
StringBuffer 线程安全的 加锁 效率低 可变字符串 适合大量拼接操作
StringBuilder 线程不安全的 可变字符串 适合大量拼接操作 效率最高
Random类
import java.util.Random;
public class Demo2 {
public static void main(String[] args) {
Random r = new Random();
int n = r.nextInt();//随机取一个数
System.out.println(n);
int t = r.nextInt(20); //随机取出 0-19之间的随机
System.out.println(t);
}
}
Date类
import java.util.Date;
public class Demo3 {
public static void main(String[] args) {
/*
获取当前系统的时间
*/
Date date = new Date();
long t = date.getTime();//1642943772783当前时间 至1970-1-1 0:0:0 时间差(毫秒单位)
System.out.println(t);
System.out.println(new Date(1642943772783L));
// @Deprecated标注此方法为过期方法,不建议使用,已经有新的方法代替,
System.out.println(date.getYear()+1900);
System.out.println(date.getMonth()+1);
System.out.println(date.getDate());
System.out.println(date.getDay());//星期天是0
}
}