正则表达式
一、概念
正则表达式是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串。其实就是一种规则,有自己特殊的应用。
二、组成规则
-
字符
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 单词边界 就是不是单词字符的地方。 举例:hello world?haha;xixi
-
Greedy 数量词
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)
四、正则表达式的分割方法
String类的功能:public String[] split(String regex)
五、正则表达式的替换功能
String类的功能:public String replaceAll(String regex,String replacement)
六、Pattern和Matcher
正则的获取功能需要使用的类。使用Pattern和Matcher类用于对正则表达式的用途进行拓展。
//使用顺序如下:
//将表达式封装进Pattern类数据中
Pattern p = Pattern.compile("a*b");
//调用匹配器
Matcher m = p.matcher("aaaaab");
boolean b = m.matches();
用法:正则表达式的获取功能,将Pattern和Matcher的结合使用。
如:获取字符串中三个字符组成的单词,使用的是 find()方法 和 group()方法 。
注意:一定要先使用find()方法先找到 才能用group()方法获取出来。
public class test {
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[a-z]{3}\\b";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
while (m.find()){
String g = m.group();
System.out.println(g.toString());
}
}
}
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类用于产生随机数。
二、构造方法
-
public Random(long seed)
给定一个long类型的种子,给定以后每一次生成的随机数是相同的。如果用相同的种子创建两个Random 实例,则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。
-
public Random()
没有给定种子,每次随机会得到不同的随机数。
三、成员方法
-
public int nextInt()
没有参数 表示的随机数范围 是int类型的范围;
-
public int nextInt(int n)
可以指定一个随机数范围 ;
-
void nextBytes(byte[] bytes)
生成随机字节并将其置于用户提供的空的 byte 数组中。
System类
一、概述
System 类包含一些有用的类字段和方法。它不能被实例化。
二、成员方法
-
public static void gc()
调用垃圾回收器;
-
public static void exit(int status)
退出java虚拟机 0 为正常退出 非0为 异常退出;
-
public static long currentTimeMillis()
获取当前时间的毫秒值(通常用于计算获取程序运行所花费的时间)。
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 :取舍模式 比如四舍五入
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]); }
Date类
一、概述
类 Date 表示特定的瞬间,精确到毫秒。
二、构造方法
-
public Date()
-
public Date(long date)
把一个long类型的毫秒值转换成一个从计算机元年(1970.1.1 0点)开始的日期对象
三、成员方法
-
public long getTime():
获取一个日期对象对象毫秒值
-
public void setTime(long time):
给一个日期对象设置上指定的毫秒值
四、Date 和 long 的相互转换
Date ----> long:
调用getTime方法
long ----> Date:
1. 构造方法
2. setTime(long time)
SimpleDateFormat类
一、概述
可以把一个日期对象格式化成一个文本(字符串) , 也可以把一个日期字符串解析成一个日期对象。
二、构造方法
-
public SimpleDateFormat():
使用默认的模式来创建一个SimpleDateFormat对象
-
public SimpleDateFormat(String pattern):
使用指定的模式(规则比如yyyy:MM:dd HH:mm:ss)来创建一个SimpleDateFormat对象
y 年
M 月
d 天
H 时
m 分
s 秒
三、成员方法
-
public String format(Date date):
把一个日期对象格式化成一个字符串;
-
public Date parse(String dateStr):
把一个日期字符串解析成一个日期对象 注意要以指定格式解析。
注意:使用此方法时必须使用alt+Enter键进行修正抛出一个异常,否则编译不通过。
Calendar类
一、概述
Calendar 类是一个抽象类,不能直接new对象,可以通过他的一个静态成员方法getInstance()来获取他的对象。
二、成员方法
-
public static Calendar getInstance()
使用默认时区和语言环境获得一个日历对象
-
public int get(int field)
获得给定日历字段对应的值 field通过Calendar提供的字段来拿
-
public void add(int field,int amount)
根据日历的规则,为给定的日历字段添加或减去指定的时间量
-
public final void set(int year,int month,int date)
设置日历时间 年月日