正则表达式 , 包装类 , Math ,Random , System , BigInteger , BigDecimal , Date , DateFormat , Calendar

正则表达式

概念: 使用单个字符串来描述/匹配一系列符合某个语法规则的字符串
可以通过正则表达式处理字符串复杂的查找/替换/匹配/分割工作

使用步骤

1.通过大量的字符串找规律定义规则
2.使用这种规则去匹配新的字符串
3.匹配成功作出相应的操作(匹配 查找 替换 分割)
正则表达式由两种基本字符组成
原义字符:字符本身就是一个正则 \t \n \r \f
元字符: * + ? $ ^ () [] {}…

正则表达式进行分类

字符类: [abc] 将字符进行归类,可以出现[]中的其中一个 对abc其中一个进行匹配
[^abc] 对不是abc的字符进行匹配

范围类:
[a-z] 表示代表a-z中的一个字符
表示所有的英文字母和数字 [a-zA-Z0-9]
预定义类:
\d == [0-9] 数字
\D == [^0-9] 非数字
空白字符:[ \t\n\x0B\f\r] == \s
[^ \t\n\x0B\f\r] == \S
[a-zA-Z0-9] \w
[^a-zA-Z0-9] \W
. 任何字符(与行结束符可能匹配也可能不匹配)

边界字符
^:以XXX开头
$:以XXX结尾
\b:单词边界
\B:非单词边界

量词
?:出现0次或者1次
+:出现1次或者多次
*:出现任意次
{n}:出现正好n次
{n,m}出现n-m次
{n,}出现至少n次

分组 ()
如何让Jack出现至少3次
(Jack){3,}
忽略分组:每一组能够分组,但是没有编号 ?:


Ja(ck|Love)Kitty

反向引用
利用分组的编号进行反向引用
反向引用使用$,必须先分组
2018-04-27 ==> 04/27/2018

public class Test04 {
	public static void main(String[] args) {
//		String regex = "\t";
//		String s = "a	b2	c3	def";
//		String replace1 = s.replaceFirst(regex, "X");//把字符串中的tab键用X替换
//		System.out.println(replace1);//aXb2	c3	def
		
//		String regex = "[abc]";
//		String s = "a	b2	c3	def";
//		String replace1 = s.replaceAll(regex, "X");//把字符串中的abc用X替换
//		System.out.println(replace1);//X	X2	X3	def
		
//		String regex = "[^abc]";
//		String s = "a	b2	c3	def";
//		String replace1 = s.replaceAll(regex, "X");//把字符串中除abc外的都用X替换
//		System.out.println(replace1);//aXbXXcXXXXX
		
//		String regex = "\\d";
//		String s = "a	b2	c3	def";
//		String replace1 = s.replaceAll(regex, "X");//把字符串中的数字都用X替换
//		System.out.println(replace1);//a	bX	cX	def
		
//		String regex = "\\.";
//		String s = "a	b2	.c3	def";
//		String replace1 = s.replaceAll(regex, "X");//把字符串中的"."用X替换
//		System.out.println(replace1);//a	b2	Xc3	def
		
//		String regex = "\\b";
//		String s = "Hello World Java";
//		String replace1 = s.replaceAll(regex, "X");//把字符串中的单词边界用X替换
//		System.out.println(replace1);//XHelloX XWorldX XJavaX
		
		// 2018-04-27 ==> 04/27/2018
		String regex = "(\\d{4})-(\\d{2})-(\\d{2})";
		String s = "2018-04-27";
		String replace = s.replaceAll(regex, "$2/$3/$1");
		System.out.println(replace);
	}
}	
正则表达式的应用总结

1.字符串查找操作 Pattern和Matcher
2.字符串匹配操作 字符串的matches()方法
3.字符串替换操作 字符串的replaceAll()和replaceFirst()方法
4.字符串分割 split() 方法

Pattern和Matcher类的使用

指定为字符串的正则表达式必须首先被编译为此类的实例。然后,可将得到的模式用于创建 Matcher 对象
依照正则表达式,该对象可以与任意字符序列匹配。执行匹配所涉及的所有状态都驻留在匹配器中,所以多个匹配器可以共享同一模式。
Pattern: 正则表达式的模型
Matcher: 匹配一系列字符串的匹配器

public class RegexDemo05 {
	public static void main(String[] args) {
//		String regex = "x?y*z";
//		String s = "xyyyz";
//		boolean bool = s.matches(regex);
//		System.out.println(bool);
		
		// 定义规则
		String regex = "x?y*z";
		// 把正则表达式编译成模型
		// static Pattern compile(String regex) 
		 Pattern p = Pattern.compile(regex);
		 Matcher m = p.matcher("xyyyz");
		 boolean result = m.matches();
		 System.out.println(result);
	}
}

小案例:把字符串的将is替换替换成大写的IS

public class RegexDemo07 {
	public static void main(String[] args) {
		String s = "He is a man\r\n" + 
				"This is a boy\r\n" + 
				"is that your car?\r\n" + 
				"isn’t it?\r\n" + 
				"What is your name?";
		
		System.out.println(replaceStr(s));
	}

	public static String replaceStr(String s) {
		String regex = "\\bis\\b";
		String result = s.replaceAll(regex, "IS");
		return result;
	}
}

包装类

八大基本数据类型
byte short int long float double char boolean
对应的包装类
Byte Short Integer Long Float Double Character Boolean

基本数据类型的包装类可以使用基本类型对应的类的相关成员变量和成员方法,相比于基本数据类型,包装类更易于使用
各个包装类的用方法与用法其实都差不多!!!!

Integer
 public int intValue()  拆箱,将Integer转化为整型
 public static int parseInt(String s)  将字符串转换为int类型
 public static String toString(int i)  将整型转换为字符串
 public static Integer valueOf(int i)  将整型装换为Integer类型
 public static Integer valueOf(String s)  将字符串转换为Integer类型
	public class IntegerTest01 {
		public static void main(String[] args) {
			System.out.println(Integer.MAX_VALUE);
			System.out.println(Integer.MIN_VALUE);
			
			
			int i = 100;
			// Integer(int value) 
			Integer ii = new Integer(i);
			int intValue = ii.intValue();
			System.out.println(intValue);
			// Integer(String s)
			Integer ii2 = new Integer("100");
			System.out.println(ii2);
			System.out.println("--------------------------");
			//  public static int parseInt(String s)  将字符串转换为int类型
			int parseInt = Integer.parseInt("100");
			System.out.println(parseInt);
			
			// public static String toString(int i)  将整型转换为字符串
			String s = Integer.toString(200);
			System.out.println(s);
			System.out.println(200 + "");
			System.out.println(String.valueOf(200));
			
			// public static Integer valueOf(String s)  将字符串转换为Integer类型
			Integer value = Integer.valueOf("200");
			System.out.println(value.intValue());
			
			
			
		}
	}

String和int的相互转换

String - int
int - String

总结

int - String
String s = i + ""; String s3 = String.valueOf(i);
 String - int
int int1 = Integer.parseInt(str);
public class IntegerTest02 {
	public static void main(String[] args) {
		// int - String
		int i = 100;
		String s = i + "";
		String s2 = Integer.toString(i);
		String s3 = String.valueOf(i);
		
		// String - int
		String str = "100";
		int int1 = Integer.parseInt(str);
		int int2 = new Integer(str).intValue();
		int int3 = Integer.valueOf(str).intValue();
		
		
	}
}

Integer的进制转换
进制转换
十进制到其他进制
public static String toBinaryString(int i)//转为2进制
public static String toOctalString(int i)//转为8进制
public static String toHexString(int i)//转为16进制
public static String toString(int i,int radix)//转为其他进制最大只能36进制
其他进制到十进制
public static int parseInt(String s,int radix)

	public class IntegerDemo03 {
		public static void main(String[] args) {
			// 十进制到其他进制
			System.out.println(Integer.toBinaryString(100));
			System.out.println(Integer.toOctalString(100));
			System.out.println(Integer.toHexString(100));
			System.out.println(Integer.toString(100, 36));
			
			// 其他进制到十进制
			System.out.println(Integer.parseInt("100", 8));
			System.out.println(Integer.parseInt("ZZ", 36));
			
			// 进行任意进制的转换 (练习: 编写一个方法能够处理任意进制的转换)
			int result = getResult(36, 8, "zz");
			System.out.println(result);
		}
		
		public static int getResult(int x, int y, int num) {
			// 将X进制转换成十进制
			int i = Integer.parseInt(String.valueOf(num), x);
			// 将十进制转换成对应的Y进制
			String s = Integer.toString(i, y);
			int result = Integer.parseInt(s);
			return result;
		}

		public static int getResult(int x, int y, String num) {
			// 将X进制转换成十进制
			int i = Integer.parseInt(num, x);
			// 将十进制转换成对应的Y进制
			String s = Integer.toString(i, y);
			int result = Integer.parseInt(s);
			return result;
		}
	}

自动拆装箱

JDK5.0以后的特性
自动拆箱: 将包装类转换成包装类对应的基本数据类型 iii.intValue()
自动装箱: 将基本数据类型转换成对应的包装类类型 Integer iii = Integer.valueOf(100);

public class IntegerDemo04 {
	public static void main(String[] args) {
		int i = 100;
		Integer ii = new Integer(i);
		
		Integer iii = 100; // 	Integer iii = Integer.valueOf(100);
		System.out.println(iii); // iii = Integer.valueOf(iii.intValue() + 200);
		
		// 先拆箱再装箱
		iii += 200;
		
//		Integer iiii = null;
//		if (iiii != null) {
//			iiii += 300;
//		}
		
		Double double1 = new Double(2.5);
		
		System.out.println(double1.doubleValue());
		
		double parseDouble = Double.parseDouble("3.6");
		
		Boolean boolean1 = new Boolean(false);
		boolean booleanValue = boolean1.booleanValue();
		System.out.println(booleanValue);
		
	}
}
Character

Character类概述:Character类是char的包装类。
构造方法

public Character(char value)

成员方法

public static boolean isUpperCase(char ch)  判断字符是否是大写
public static boolean isLowerCase(char ch)  判断字符是否是小写
public static boolean isDigit(char ch)  判断字符是否是数字
public static char toUpperCase(char ch)  将字符转大写
public static char toLowerCase(char ch)  将字符转小写 	
public class CharacterDemo01 {
	public static void main(String[] args) {
		
		System.out.println(Character.isUpperCase('A'));
		System.out.println(Character.isLowerCase('a'));
		System.out.println(Character.isDigit('0'));
		
		System.out.println(Character.toUpperCase('a'));
		System.out.println(Character.toLowerCase('A'));
		
		System.out.println(Character.isLetter('u'));
		System.out.println(Character.isJavaIdentifierStart('_'));
	}
}

Math

Math类概述:Math 类涵盖了很多执行基本数学运算的方法, 是一个工具类。
成员方法

public static int abs(int a)  求绝对值
public static double sqrt(double 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()  生成随机数
public static int round(float a)  四舍五入
	public class MathDemo01 {
		public static void main(String[] args) {
			System.out.println(Math.abs(-2));
			System.out.println(Math.sqrt(4));
			System.out.println(Math.ceil(5.2));
			System.out.println(Math.floor(5.8));
			System.out.println(Math.max(Math.max(11, 20),Math.max(8, 10)));
			System.out.println(Math.pow(2, 3));
			System.out.println(Math.round(2.4));
			
			// 	public static double random()  生成随机数
			System.out.println(Math.random());
			System.out.println((int)(Math.random()*100) + 100);
			
			String s = "abc";
			String intern = s.intern();
			System.out.println(intern);
			
			System.out.println(getRandomNum(20, 30));
		}

Random

Random类概述:生成随机数
构造方法

public Random()
public Random(long seed)

成员方法

  public int nextInt() 生成int范围内的随机数
  public int nextInt(int n)	生成[0,n)的随机整数
	public class RandomDemo01 {
		public static void main(String[] args) {
	//		Random random = new Random();
	//		for (int i = 0; i < 1000; i++) {
				int num = random.nextInt();
	//			int num = random.nextInt(10);
	//			System.out.println(num);
	//		}
			
			// 1000 7564  
			// 1001 3340
			Random random = new Random(1001L);
			for (int i = 0; i < 10; i++) {
				int num = random.nextInt(10);
				System.out.println(num);
			}
		}
	}

System

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

public static void gc()  运行垃圾回收器
public static void exit(int status)  终止当前正在运行的 Java 虚拟机。参数用作状态码;
根据惯例,非 0 的状态码表示异常终止
public static long currentTimeMillis()  返回以毫秒为单位的当前时间
	public class SystemDemo01 {
		public static void main(String[] args) {
	//		System.gc();
			
	//		for (int i = 0; i < 100; i++) {
	//			System.out.println(i);
	//			System.exit(1);
	//		}
			
	//		public static long currentTimeMillis()  返回以毫秒为单位的当前时间
			long times = System.currentTimeMillis();
			System.out.println(times);
			
			Date date = new Date(1524879435811L);
			System.out.println(date);
			
		}
	}

BigInteger

BigInteger类概述:对于超出int范围内的数据进行运算

BigInteger类构造方法
public BigInteger(String val)
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

在一些项目当中,在进行小数运算的时,float、double很容易丢失精度为了能精确的表示、计算浮点数,Java设计了BigDecimal。
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 class BigDecimalDemo01 {
	public static void main(String[] args) {
		System.out.println(1.0 - 0.33);
		System.out.println(0.01 + 0.09);
		System.out.println(1.1015 * 100);
		System.out.println(1.301 / 100);
		
		BigDecimal bd = new BigDecimal("0.01");
		BigDecimal addResult = bd.add(new BigDecimal("0.09"));
		System.out.println(addResult);
		
		System.out.println(new BigDecimal("1.0").subtract(new BigDecimal("0.33")));
		
		System.out.println(new BigDecimal("1.1015").multiply(new BigDecimal("100")));
		
		System.out.println(new BigDecimal("1.301").divide(new BigDecimal("100")));
	}
}

Date

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

public Date()  根据当前的默认毫秒值创建日期对象
public Date(long date)  根据给定的毫秒值创建日期对象

成员方法

public long getTime()  获取时间,以毫秒为单位
public void setTime(long time))  设置时间		
public class DateDemo01 {
	public static void main(String[] args) {
		Date date = new Date();
		long time = date.getTime();
		System.out.println(date);
		
		Date date2 = new Date(time);
		System.out.println(date2);
	}
}

DateFormat

DateFormat类概述:DateFormat 是日期/时间格式化子类的抽象类,
它以与语言无关的方式格式化并解析日期或时间y因为DateFormate是抽象类,所以可以使用该类的子类SimpleDateFormat 。
构造方法

public SimpleDateFormat()
public SimpleDateFormat(String pattern)

成员方法

public final String format(Date date)
public Date parse(String source)
解析: parse
	文本内容(字符串  xml json html) --> 实体类  解析
格式化: formate
	实体类 --> 文本内容(字符串  xml json html)
public class SimpleDateFormate {
	public static void main(String[] args) throws ParseException {
		Date date = new Date();
		// 2018-05-03  
//		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
		
		String s = dateFormat.format(date);
		System.out.println(s);
		
		String dateStr = "2019/08/06 23:12:50";
		DateFormat dateFormat2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date date2 = dateFormat2.parse(dateStr);
		System.out.println(date2);
	}
}

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 class CalendarDemo01 {
	public static void main(String[] args) {
		Calendar c = Calendar.getInstance();
		System.out.println(c);
//		long time = c.getTime().getTime();
//		System.out.println(time);
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH);
		int day = c.get(Calendar.DAY_OF_MONTH);
		int hour = c.get(Calendar.HOUR_OF_DAY);
		int minute = c.get(Calendar.MINUTE);
		int second = c.get(Calendar.SECOND);
		System.out.println(year + "年" + (month + 1) + "月" + day);
		
		/*
		 * public void add(int field,int amount)
		   public final void set(int year,int month,int date)	
		 */
		c.add(Calendar.YEAR, -3);
		System.out.println(c);
		
		int year2 = c.get(Calendar.YEAR);
		int month2 = c.get(Calendar.MONTH);
		int day2 = c.get(Calendar.DAY_OF_MONTH);
		System.out.println(year2 + "年" + (month2 + 1) + "月" + day2);
		
		c.add(Calendar.YEAR, 30);
		
		int year3 = c.get(Calendar.YEAR);
		int month3 = c.get(Calendar.MONTH);
		int day3 = c.get(Calendar.DAY_OF_MONTH);
		System.out.println(year3 + "年" + (month3 + 1) + "月" + day3);
		
		// public final void set(int year,int month,int date)
		c.set(5050, 5, 20, 12, 12, 12);
		int year4 = c.get(Calendar.YEAR);
		int month4 = c.get(Calendar.MONTH);
		int day4 = c.get(Calendar.DAY_OF_MONTH);
		System.out.println(year4 + "年" + (month4 + 1) + "月" + day4);
	}
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值