Day13
一、正则表达式(了解)
理解:用来描述或者匹配一系列符合某个语句规则的字符串
需求:替换字符串中的电话号码
public class Test {
public static void main(String[] args) {
String str = "坤坤15112347899 凡凡18512347894";
String regex = "(1\\d{2}(\\d{4})(\\d{4}))";
str = str.replaceAll(regex, "$1****$3");
System.out.println(str);
}
}
代码解释:
- 定义字符串变量
str
String str = "坤坤15112347899 凡凡18512347894";
- 定义正则表达式
regex
String regex = "(1\\d{2}(\\d{4})(\\d{4}))";
这个正则表达式的含义如下:
1
:匹配以1开头的字符。\\d{2}
:匹配两位数字(\d
表示任意数字,{2}
表示恰好两位)。(\\d{4})
:匹配四位数字,并将其捕获到第一个捕获组中。(\\d{4})
:匹配接下来的四位数字,并将其捕获到第二个捕获组中。整个正则表达式匹配的是一个完整的11位电话号码,并且将中间四位数字捕获到第一个捕获组中,最后四位数字捕获到第二个捕获组中。
- 使用
replaceAll
方法进行替换str = str.replaceAll(regex, "$1****$3");
replaceAll
方法使用正则表达式进行匹配,并将匹配到的部分替换为指定的字符串。这里的替换字符串是$1****$3
:
$1
:表示第一个捕获组的内容,即电话号码的前7位。****
:用四个星号替换中间的四位数字。$3
:表示第三个捕获组的内容,即电话号码的后四位。最终输出的结果将是:
坤坤151****7899 凡凡185****7894
小结:可以利用正则表达式对字符串进行 替换
需求:校验QQ邮箱
pubcic static void main(String[] args) {
String email = "1234567891@qq.com";
String regex = "\\d{6,11}"@qq.com;
boolean matches = email.matches(regex);
System.out.println(matches);
}
小结:可以利用正则表达式对字符串进行 校验
需求:分隔路径
public static void main(String[] args) { String str = "D:\\music\\坤坤music.mp3"; Sring regex = ":?\\\\";// :\\ 或\\ String[] split = str.split(regex); for (String string : split) { System.out.println(String); } }
需求:爬数据
public static void main(String[] args) { String str = "<img src= 'hhy/aaa.jpg'/><div><div/> <input type='image' src='submit.gif' /><img src='bbb.jpg'/>"; //正则表达式的字符串 String regex = "<img\\b[^>]*\\bsrc\\b\\s*=\\s*('|\")?([^'\"\n\r\f>]+(\\.jpg)\\b)[^>]*>"; //正则表达式的对象 Pattern pattern = Pattern.compile(regex); //利用pattern(正则表达式的对象) 解析 str(数据源),返回matcher(匹配结果对象) Matcher matcher = pattern.matcher(str); //循环查找匹配结果 while(matcher.find()){ //获取匹配结果中的第2组 String group = matcher.group(2); System.out.println(group); } }
二、日期时间类
分类:
- Date 日期类
- SimpleDateFormat 格式化日期类
- Calendar 日历类
Date
public static void main(String[] args) {
Date date = new Date();
//星期 月份 日期 时:分:秒 时区 年份
//Wed Nov 20 09:39:01 CST 2024
System.out.println(date)
}
SimpleDateFormat
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
//将Date对象 转换为 符合格式的字符串
String datetime = sdf.format(new Date());
System.out.println(datetime);
//将符合格式的字符串 转换为 Date对象
Date date = sdf.parse(datetime);
System.out.println(date);
}
Calendar
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;//月份是0~11,所以要+1
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendear.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
System.out.println(year);
System.out.println(month);
System.out.println(day);
System.out.println(minute);
System.out.println(hour);
System.out.println(second);
}
三、Math类及静态导入
Math类
public static void main(String[] args) {
System.out.println("求次方:" + Math.pow(3,2));//9.0
System.out.println("求平方根:" + Math.sqrt(9));//3.0
System.out.println("求绝对值:" + Math.abs(-100));
System.out.printlnJ("向上取整(天花板):" + Math.ceilJ(1.1));
System.out.println("向下取整(地板):" + Math.floor(1.9));
System.out.println("求最大值:" + Math.max(10,20));
System.out.println("求最小值:" + Math.min(10,29));
System.out.println("求四舍五入:" + Math.round(1.5));
System.out.println("获取随机数(0包含~1排他):" + Math.random);
}
需求:获取1~100的随机数
public static void main(String[] args) { int num = (int)(Math.random()*100) + 1; System.out.println("获取1~100的随机数:" + num); }
面试题:Math.abs() 是否会返回负数?
public static void main(String[] args) { //System.out.println(Integer.MAX_VALUE);//214774833647 --- 2的31次方-1 //System.out.println(Integer.MIN_VALUE);//-2147483648--- -2的31次方 System.out.println("求绝对值:" + Math.abs(Integer.MIN_VALUE)); System.out.println("求绝对值:" + Math.abs(Integer.MAX_VALUE + 1)); }
静态导入
//将该类里所有的静态属性和静态方法导入到本类里,成为本类自己的静态属性和静态方法
import static java.lang.Math.*;
public class Test03 {
/**
* 知识点:静态导入
*/
public static void main(String[] args) {
System.out.println("求绝对值:" + abs(-100));//100
}
}
四、Random
Random - 随机类
public class Test { public static void main(String[] args) { Random random = new Random(); System.out.println("随机出int取值范围内的数据:" + random.nextInt()); System.out.println("随机出float取值范围内的数据:" + random.nextFloat()); System.out.println("随机出double取值范围内的数据:" + random.nextDouble()); System.out.println("随机出boolean取值范围内的数据:" + random.nextBoolean()); System.out.println("随机出0~9的int值:" + random.nextInt(10)); } }
public static void main(String[] args) { Random random = new Random(); String[] names = {"蔡徐坤", "坤坤", "凡凡", "鸡哥"}; int index = random.nextInt(names.length); System.out.println(names[index]); }
public static void main(String[] args) { Random ran1 = new Random(); System.out.println(ran1.nextInt()); System.out.println(ran1.nextInt(10)); System.out.println("------------------------"); //注意:种子数固定了,随机数也固定了 Random ran2 = new Random(10); System.out.println(ran2.nextInt()); System.out.println(ran2.nextInt(10)); System.out.println("------------------------"); MyRandom myRandom1 = new MyRandom(); System.out.println(myRandom1.nextInt()); System.out.println(myRandom1.nextInt(10)); System.out.println("------------------------"); MyRandom myRandom2 = new MyRandom(10); System.out.println(myRandom2.nextInt()); System.out.println(myRandom2.nextInt(10)); }
public class MyRandom { private long seed;//种子数 public MyRandom() { this(seedUniquifier() ^ System.nanoTime()); } public MyRandom(long seed) { this.seed = seed; } private static long seedUniquifier() { for (;;) { long current = System.currentTimeMillis(); long next = current * 181783497276652981L; if (next%3==0 || next$%17==0 || next%38==0) { return next; } } public int nextInt() { return (int) seed; } public int nextInt(int val) { return Math.abs((int) seed)%val; } } }
五、Runtime
Runtime - 运行时环境
public static void main(String[] args) { Runtime run = Runtime.getRuntime(); System.out.println("获取处理数:" + run.availabliProcessors()); System.out.println("获取最大内存数(字节):" + run.maxMemory()); System.out.println("获取限制内存数(字节):" + run.freeMemory());
考虑程序运行效率 (1.运行时长 2.消耗内存
public class Test { public static void main(String[] args) { Runtime run = Runtime.getRuntime(); long startMemory = run.freeMemory(); long startTime = System.currentTimeMillis(); StringBuilder sb = new StringBuilder(11000000); sb.append("坤坤"); for (int i = 0; i < 1000000; i++) { sb.append("唱跳rape篮球"); } long endMemory = run.freeMemory(); long endTime = System.currentTimeMillis(); System.out.println("消耗时长为:" + (endTime - startTime)); System.out.println("消耗内存为:" + (startMemory - endMemory)); } }
总结:
正则表达式 – 了解
日期类
- Date + SimpleDateFormet
- Calendar
Math类
注意:静态导入
Random类
注意:理解种子数
Runtime类