1:正则表达式(理解)
(1)就是符合一定规则的字符串
(2)常见规则
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] 0到9的字符都包括
C:预定义字符类
. 任何字符。我的就是.字符本身,怎么表示呢? \.
\d 数字:[0-9]
\w 单词字符:[a-zA-Z_0-9]
在正则表达式里面组成单词的东西必须有这些东西组成
D:边界匹配器
^ 行的开头
$ 行的结尾
\b 单词边界
就是不是单词字符的地方。
举例:hello world?haha;xixi
E:Greedy 数量词
X? X,一次或一次也没有
X* X,零次或多次
X+ X,一次或多次
X{n} X,恰好 n 次
X{n,} X,至少 n 次
X{n,m} X,至少 n 次,但是不超过 m 次
(3)常见功能:
- A:判断功能
String类的public boolean matches(String regex) - B:分割功能
String类的public String[] split(String regex) - C:替换功能
String类的public String replaceAll(String regex,String replacement) - D:获取功能
Pattern和Matcher
Pattern p = Pattern.compile(“a*b”);
Matcher m = p.matcher(“aaaab”);
find():查找存不存在
group():获取刚才查找过的数据
案例:判断电话号码和邮箱
package cn.itcast_02;
import java.util.Scanner;
/*
* 判断功能
* String类的public boolean matches(String regex)
*
* 需求:
* 判断手机号码是否满足要求
*
* 分析:
* A:键盘录入手机号码
* B:定义手机号码的规则
* C:调用功能,判断即可
* D:输出结果
*/
public class RegexDemo {
public static void main(String[] args) {
// 键盘录入手机号码
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的手机号码:");
String phone = sc.nextLine();
// 定义手机号码规则
String regex = "1[38]\\d{9}";
// 调用功能,判断即可
boolean flag = phone.matches(regex);
// 输出结果
System.out.println("flag:" + flag);
}
}
package cn.itcast_02;
import java.util.Scanner;
/*
* 校验邮箱
*
* 分析:
* A:键盘录入邮箱
* B:定义邮箱的规则
* 1517806580@qq.com
* liuyi@163.com
* linqingxia@126.com
* fengqingyang@sina.com.cn
* fqy@itcast.cn
* C:调用功能,判断即可
* D:输出结果
*/
public class RegexTest {
public static void main(String[] args) {
// 键盘录入邮箱
Scanner sc = new Scanner(System.in);
System.out.println("请输入邮箱:");
String email = sc.nextLine();
// 定义邮箱的规则
String regex = "\\w+@\\w{2,6}(\\.\\w{2,3})+";
// 调用功能,判断即可
boolean flag = email.matches(regex);
// 输出结果
System.out.println("flag:" + flag);
}
}
按照不同的规则分割数据
package cn.itcast_03;
import java.util.Scanner;
/*
* 分割功能
* String类的pubic String[] split(String regex)
* 根据给定正则表达式的匹配拆分此字符串
*
* 举例:
* 百合网,世纪佳缘,珍爱网,QQ
* 搜索好友
* 性别:女
* 范围:18-24
*
* age>=18 && age<=24
*/
public class RegexDemo {
public static void main(String[] args) {
// 定义一个年龄搜索范围
String ages = "18-24";
// 定义规则
String regex = "-";
// 调用方法
String[] strArray = ages.split(regex);
// 如何得到int类型
int startAge = Integer.parseInt(strArray[0]);
int endAge = Integer.parseInt(strArray[1]);
// 键盘录入年龄
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的年龄:");
int age = sc.nextInt();
if (age>=startAge && age<=endAge) {
System.out.println("你就是我想要找的");
} else {
System.out.println("不符合我的要求,gun");
}
}
}
package cn.itcast_03;
import java.util.Arrays;
/*
* 我有如下一个字符串:"91 27 46 38 50"
* 请写代码实现最终输出的结果是:"27 38 46 50 91"
*
* 分析:
* A:定义一个字符串
* B:把字符串进行分割,得到一个字符数组
* C:把字符串数组变换成int数组
* D:对int数组排序
* E:把排序后的int数组再组装成一个字符串
* F:输出字符串
*/
public class RegexTest {
public static void main(String[] args) {
// 定义一个字符串
String s = "91 27 46 38 50";
// 把字符串进行分割,得到一个字符串数组
String[] strArray = s.split(" ");
// 把字符串数组变换成int数组
int[] arr = new int[strArray.length];
;
for (int x = 0; x < arr.length; x++) {
arr[x] = Integer.parseInt(strArray[x]);
}
// 对int数组排序
Arrays.sort(arr);
// 把排序后的int数组再组装成一个字符串
StringBuilder sb = new StringBuilder();
for (int x = 0; x < arr.length; x++) {
sb.append(arr[x]).append(" ");
}
// 转化为字符串
// String result = String.valueOf(sb).trim();
String result = sb.toString().trim();
// 输出字符串
System.out.println("result:" + result);
}
}
把论坛中的数字替换为*或者去掉
package cn.itcast_04;
/*
*
* 替换功能
* String类的public String replaceAll(String regex,String replacement)
* 使用给定replacement 替换此字符串所有匹配给定的正则表达式的子字符串
*/
public class RegexDemo {
public static void main(String[] args) {
// 定义一个字符串
String s = "helloqq12345worldkh622112345678java";
// 要去除所有的数字,用*号替换
// String regex = "\\d+";
// String regex = "\\d+";
// String ss = "*";
// 直接把数字去掉
String regex = "\\d+";
String rep = "";
String result = s.replaceAll(regex, rep);
System.out.println(result);
}
}
获取字符串中由3个字符组成的单词
package cn.itcast_05;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*
* 获取功能:
* 获取下面这个字符串中由三个字符组成的单词
* da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?
*/
public class RegexDemo2 {
public static void main(String[] args) {
// 定义字符串
String s = "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(s);
// 调用匹配器对象的功能
// 通过find方法就是查找有没有满足条件的子串
// // public boolean find()
// boolean flag = m.find();
// System.out.println(flag);
// // 如何得到值呢?
// // public String group()
// String ss = m.group();
// System.out.println(ss);
// 使用循环方法
while(m.find()) {
System.out.println(m.group());
}
// 注意:一定要先find(),然后才能group(),否则报错
// IllegalsStateException:No match found
// String ss = m.group();
// System.out.println(ss);
}
}
2:Math(掌握)
- (1)针对数学运算进行操作的类
- (2)常见方法
A:绝对值:public static int abs(int a)
B:向上取整:public static double ceil(double a)
C:向下取整:public static double floor(double a)
D:两个数据中的大值:public static int max(int a,int b)
E:a的b次幂:public static double pow(double a,double b)
F:随机数[0.0,1.0):public static double random()
G:四舍五入,原理,任何数+1/2 后取整:public static int round(float a)
H:正平方根:public static double sqrt(double a)
3:Random(理解)
- (1)用于产生随机数的类
- (2)构造方法:
A:Random() 默认种子,每次产生的随机数不同
B:Random(long seed) 指定种子,每次种子相同,随机数就相同 - (3)成员方法:
A:int nextInt() 返回int范围内的随机数
B:int nextInt(int n) 返回[0,n)范围内的随机数
4:System(掌握)
- (1)系统类,提供了一些有用的字段和方法
- (2)成员方法
A:运行垃圾回收器:public static void gc()
B:退出jvm:public static void exit(int status)
C:获取当前时间的毫秒值:public static long currentTimeMillis()
D:数组复制:public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
5:BigInteger(理解)
- (1)针对大整数的运算
- (2)构造方法
A:BigInteger(String s) - (3)成员方法
A:加:public BigInteger add(BigInteger val)
B:减:public BigInteger subtract(BigInteger val)
C:乘:public BigInteger multiply(BigInteger val)
D:除:public BigInteger divide(BigInteger val)
E:商和余数:public BigInteger[] divideAndRemainder(BigInteger val)
举例:
package cn.itcast_02;
import java.math.BigInteger;
public class BigIntegerDemo {
public static void main(String[] args) {
BigInteger bi1 = new BigInteger("100");
BigInteger bi2 = new BigInteger("50");
// public BigInteger add(BigInteger val):加
System.out.println("add:" + bi1.add(bi2));
// public BigInteger subtract(BigInteger val):减
System.out.println("subtract:" + bi1.subtract(bi2));
// public BigInteger multiply(BigInteger val):乘
System.out.println("multiply:" + bi1.multiply(bi2));
// public BigInteger divide(BigInteger val):除
System.out.println("divide:" + bi1.divide(bi2));
// public BigInteger[] divideAndRemainder(BigInteger val):返回商和余数的数组
BigInteger[] bis = bi1.divideAndRemainder(bi2);
System.out.println("商:" + bis[0]);
System.out.println("余数:" + bis[1]);
}
}
6:BigDecimal(理解)
- (1)浮点数据做运算,会丢失精度。所以,针对浮点数据的操作建议采用BigDecimal。(金融相关的项目)
- (2)构造方法
A:BigDecimal(String s) - (3)成员方法:
A:加:public BigDecimal add(BigDecimal augend)
B:减:public BigDecimal subtract(BigDecimal subtrahend)
C:乘:public BigDecimal multiply(BigDecimal multiplicand)
D:除:public BigDecimal divide(BigDecimal divisor)
E:自己保留小数几位:public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode):商,几位小数,如何取舍
举例:
package cn.itcast_02;
import java.math.BigDecimal;
public class BigDecimalDemo {
public static void main(String[] args) {
BigDecimal bd1 = new BigDecimal("0.09");
BigDecimal bd2 = new BigDecimal("0.01");
System.out.println("add:" + bd1.add(bd2));
System.out.println("-------------------");
BigDecimal bd3 = new BigDecimal("1.0");
BigDecimal bd4 = new BigDecimal("0.32");
System.out.println("subtract:" + bd3.subtract(bd4));
System.out.println("-------------------");
BigDecimal bd5 = new BigDecimal("0.09");
BigDecimal bd6 = new BigDecimal("0.01");
System.out.println("multiply:" + bd5.multiply(bd6));
System.out.println("-------------------");
BigDecimal bd7 = new BigDecimal("1.301");
BigDecimal bd8 = new BigDecimal("100");
System.out.println("divide:" + bd7.divide(bd8));
System.out.println("divide:" + bd7.divide(bd8,5,BigDecimal.ROUND_HALF_UP));
}
}
输出结果:
add:0.10
-------------------
subtract:0.68
-------------------
multiply:0.0009
-------------------
divide:0.01301
divide:0.01301
7:Date/DateFormat(掌握)
(1)Date是日期类,可以精确到毫秒。
- A:构造方法
Date()
Date(long time) - B:成员方法
getTime()
setTime(long time)
C:日期和毫秒值的相互转换
案例:你来到这个世界多少天了?
package cn.itcast_05;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
/*
* 算一下你来到这个世界多少天
*
* 分析:
* A:键盘录入你的出生的年月日
* B:把该字符串转换为一个日期
* C:通过该日期得到一个毫秒值
* D:获取当前时间的毫秒值
* E:用D—C得到一个毫秒值
* F:把E的毫秒值转换为年
* /1000/60/60/24
*/
public class MyYearOldDemo {
public static void main(String[] args) throws ParseException {
// 键盘录入你的出生年月日
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的出生年月:");
String line = sc.nextLine();
// 把该字符串转为一个日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse(line);
// 通过该日期得到一个毫秒值
long myTime = d.getTime();
// 获取当前时间的毫秒值
long nowTime = System.currentTimeMillis();
// 用D-C得到一个毫秒值
long time = nowTime - myTime;
// 把E的毫秒值转换为天
long day = time/1000/60/60/24;
System.out.println("你来到这个世界:" + day + "天");
}
}
(2)DateFormat针对日期进行格式化和针对字符串进行解析的类,是抽象类,所以使用其子类SimpleDateFormat
- A:SimpleDateFormat(String pattern) 给定模式
yyyy-MM-dd HH:mm:ss - B:日期和字符串的转换
a:Date – String
format()
b:String – Date
parse()
案例:制作了一个针对日期操作的工具类。
package cn.itcast_04;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 这是日期和字符串相互转换的工具类
*
* @author ZF-Honor
*
*/
public class DateUtil {
private DateUtil() {
}
/**
* 这个方法的作用就是把日期转成一个字符串
*
* @param d
* 被转换的日期对象
*
* @Param format
* 传递过来的要被转换的格式
* @return 格式化后的字符串
*/
public static String dateToString(Date d, String format) {
// SimpleDateFormat sdf = new SimpleDateFormat(format)
// return sdf.format(d);
return new SimpleDateFormat(format).format(d);
}
/**
* 这个方法的作用就是把一个字符串解析成一个日期对象
* @param s 被解析的字符串
* @param format 传递过来的要转换的格式
* @return 解析后的日期对象
* @throws ParseException
*/
public static Date stringToDate(String s, String format) throws ParseException {
return new SimpleDateFormat(format).parse(s);
}
}
**工具类的测试:**
package cn.itcast_04;
import java.text.ParseException;
import java.util.Date;
/*
* 工具类的测试
*/
public class DateUtilDemo {
public static void main(String[] args) throws ParseException {
Date d = new Date();
// yyyy-MM-dd HH:mm:ss
String s = DateUtil.dateToString(d, "yyyy-MM-dd HH:mm:ss");
System.out.println(s);
String s2 = DateUtil.dateToString(d, "yyyy-MM-dd");
System.out.println(s2);
String str = "2014-10-14";
Date dd = DateUtil.stringToDate(str, "yyyy-MM-dd");
System.out.println(dd);
}
}
8:Calendar(掌握)
(1)日历类,封装了所有的日历字段值,通过统一的方法根据传入不同的日历字段可以获取值。
(2)如何得到一个日历对象呢?
Calendar rightNow = Calendar.getInstance();
本质返回的是子类对象
(3)成员方法
A:根据日历字段得到对应的值
B:根据日历字段和一个正负数确定是添加还是减去对应日历字段的值
C:设置日历对象的年月日
案例:计算任意一年的2月份有多少天?
package cn.itcast_03;
import java.util.Calendar;
import java.util.Scanner;
/*
* 获取任意一年的二月有多少天
*
* 分析:
* A:键盘录入任意的年份
* B:设置日历对象的年月日
* 年就是A输入的数据
* 月就是2
* 日就是1
* C:把时间往前推一天,就是2月的最后一天
* D:输出这一天即可
*/
public class CalendarTest {
public static void main(String[] args) {
// 键盘录入任意的年份
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份:");
int year = sc.nextInt();
// 设置日历对象的年月日
Calendar c = Calendar.getInstance();
c.set(year,2,1); // 其实为这一年3月的第一天
// 把时间往前推一天,就是2月的最后一天
c.add(Calendar.DATE,-1);
// 获取这一天输出即可
System.out.println(c.get(Calendar.DATE));
}
}